Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Apr 9, 2024
1 parent 9bb1325 commit 90d9e30
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 60 deletions.
34 changes: 25 additions & 9 deletions cookbook/claude/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,51 @@ export ANTHROPIC_API_KEY=xxx
### 3. Install libraries

```shell
pip install -U anthropic phidata duckduckgo-search duckdb yfinance exa_py
pip install -U anthropic duckduckgo-search duckdb yfinance exa_py phidata
```

### 4. Web search function calling
### 4. Run Assistant

- stream on

```shell
python cookbook/claude/web_search.py
python cookbook/claude/assistant.py
```

### 5. YFinance function calling
- stream off

```shell
python cookbook/claude/finance.py
python cookbook/claude/assistant_stream_off.py
```

### 6. Structured output
### 5. Run Assistant with Tools

- Web search

```shell
python cookbook/claude/structured_output.py
python cookbook/claude/web_search.py
```

- YFinance

```shell
python cookbook/claude/finance.py
```

### 7. Data Analyst
- Data Analyst

```shell
python cookbook/claude/data_analyst.py
```

### 8. Exa Search
- Exa Search

```shell
python cookbook/claude/exa_search.py
```

### 6. Run Assistant with Structured output

```shell
python cookbook/claude/structured_output.py
```
12 changes: 9 additions & 3 deletions cookbook/claude/exa_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from phi.assistant import Assistant
from phi.llm.anthropic import Claude
from phi.tools.exa import ExaTools
from phi.tools.website import WebsiteTools
from phi.llm.anthropic import Claude

assistant = Assistant(llm=Claude(), tools=[ExaTools()], show_tool_calls=True)
assistant.cli_app(markdown=True)
assistant = Assistant(llm=Claude(), tools=[ExaTools(), WebsiteTools()], show_tool_calls=True)
assistant.print_response(
"Produce this table: research chromatic homotopy theory."
"Access each link in the result outputting the summary for that article, its link, and keywords; "
"After the table output make conceptual ascii art of the overarching themes and constructions",
markdown=True,
)
10 changes: 7 additions & 3 deletions cookbook/claude/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
from phi.llm.anthropic import Claude

assistant = Assistant(
llm=Claude(model="claude-3-opus-20240229"),
name="Finance Assistant",
llm=Claude(model="claude-3-haiku-20240307"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
description="You are an investment analyst that researches stock prices, analyst recommendations, and stock fundamentals.",
instructions=["Format your response using markdown and use tables to display data where possible."],
debug_mode=True,
)
assistant.print_response("Share the NVDA stock price and some analyst recommendations", markdown=True)
assistant.print_response("Summarize fundamentals for TSLA", markdown=True)
assistant.print_response("Share the NVDA stock price and analyst recommendations", markdown=True)
# assistant.print_response("Summarize fundamentals for TSLA", markdown=True)
6 changes: 3 additions & 3 deletions cookbook/cohere/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cohere function calling
# CohereChat function calling

Currently "command-r" model supports function calling

Expand All @@ -11,7 +11,7 @@ python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate
```

### 2. Export your Cohere API Key
### 2. Export your CohereChat API Key

```shell
export CO_API_KEY=xxx
Expand All @@ -20,7 +20,7 @@ export CO_API_KEY=xxx
### 3. Install libraries

```shell
pip install -U cohere phidata duckduckgo-search yfinance exa_py
pip install -U cohere duckduckgo-search yfinance exa_py phidata
```

### 4. Web search function calling
Expand Down
6 changes: 4 additions & 2 deletions cookbook/cohere/assistant.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from phi.assistant import Assistant
from phi.llm.cohere import Cohere
from phi.llm.cohere import CohereChat

assistant = Assistant(
llm=Cohere(model="command-r"), description="You help people with their health and fitness goals.", debug_mode=True
llm=CohereChat(model="command-r"),
description="You help people with their health and fitness goals.",
debug_mode=True,
)
assistant.print_response("Share a quick healthy breakfast recipe.", markdown=True)
6 changes: 4 additions & 2 deletions cookbook/cohere/assistant_stream_off.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from phi.assistant import Assistant
from phi.llm.cohere import Cohere
from phi.llm.cohere import CohereChat

assistant = Assistant(
llm=Cohere(model="command-r"), description="You help people with their health and fitness goals.", debug_mode=True
llm=CohereChat(model="command-r"),
description="You help people with their health and fitness goals.",
debug_mode=True,
)
assistant.print_response("Share a quick healthy breakfast recipe.", markdown=True, stream=False)
4 changes: 2 additions & 2 deletions cookbook/cohere/exa_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from phi.assistant import Assistant
from phi.llm.cohere import Cohere
from phi.llm.cohere import CohereChat
from phi.tools.exa import ExaTools

assistant = Assistant(llm=Cohere(model="command-r"), tools=[ExaTools()], show_tool_calls=True)
assistant = Assistant(llm=CohereChat(model="command-r"), tools=[ExaTools()], show_tool_calls=True)
assistant.cli_app(markdown=True)
6 changes: 3 additions & 3 deletions cookbook/cohere/finance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from phi.assistant import Assistant
from phi.tools.yfinance import YFinanceTools
from phi.llm.cohere import Cohere
from phi.llm.cohere import CohereChat

assistant = Assistant(
llm=Cohere(model="command-r"),
llm=CohereChat(model="command-r"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
)
assistant.print_response("Share the NVDA stock price and some analyst recommendations", markdown=True)
assistant.print_response("Share the NVDA stock price and analyst recommendations", markdown=True)
assistant.print_response("Summarize fundamentals for TSLA", markdown=True)
4 changes: 2 additions & 2 deletions cookbook/cohere/structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pydantic import BaseModel, Field
from rich.pretty import pprint
from phi.assistant import Assistant
from phi.llm.cohere import Cohere
from phi.llm.cohere import CohereChat


class MovieScript(BaseModel):
Expand All @@ -17,7 +17,7 @@ class MovieScript(BaseModel):


movie_assistant = Assistant(
llm=Cohere(model="command-r"),
llm=CohereChat(model="command-r"),
description="You help people write movie scripts.",
output_model=MovieScript,
# debug_mode=True,
Expand Down
4 changes: 2 additions & 2 deletions cookbook/cohere/web_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from phi.assistant import Assistant
from phi.tools.duckduckgo import DuckDuckGo
from phi.llm.cohere import Cohere
from phi.llm.cohere import CohereChat

assistant = Assistant(llm=Cohere(model="command-r"), tools=[DuckDuckGo()], show_tool_calls=True)
assistant = Assistant(llm=CohereChat(model="command-r"), tools=[DuckDuckGo()], show_tool_calls=True)
assistant.print_response("Share 1 story from france and 1 from germany?", markdown=True)
7 changes: 0 additions & 7 deletions cookbook/groq/exa_search.py

This file was deleted.

5 changes: 3 additions & 2 deletions cookbook/groq/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
llm=Groq(model="mixtral-8x7b-32768"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
# debug_mode=True,
)
assistant.print_response("Share the NVDA stock price and some analyst recommendations", markdown=True, stream=False)
assistant.print_response("Summarize fundamentals for TSLA", markdown=True, stream=False)
assistant.print_response("Share the NVDA stock price and analyst recommendations", markdown=True, stream=False)
# assistant.print_response("Summarize fundamentals for TSLA", markdown=True, stream=False)
7 changes: 6 additions & 1 deletion cookbook/groq/web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
from phi.tools.duckduckgo import DuckDuckGo
from phi.llm.groq import Groq

assistant = Assistant(llm=Groq(model="mixtral-8x7b-32768"), tools=[DuckDuckGo()], show_tool_calls=True, debug_mode=True)
assistant = Assistant(
llm=Groq(model="mixtral-8x7b-32768"),
tools=[DuckDuckGo()],
show_tool_calls=True,
# debug_mode=True
)
assistant.print_response("Tell me about OpenAI Sora", markdown=True, stream=False)
2 changes: 1 addition & 1 deletion cookbook/hermes2/finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
)
assistant.print_response("Share the NVDA stock price and some analyst recommendations", markdown=True)
assistant.print_response("Share the NVDA stock price and analyst recommendations", markdown=True)
assistant.print_response("Summarize fundamentals for TSLA", markdown=True)
2 changes: 1 addition & 1 deletion phi/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,11 @@ def _run(

# -*- Update run output
self.output = run_output
logger.debug(f"*********** Run End: {self.run_id} ***********")

# -*- Yield final response if not streaming
if not stream:
yield run_output
logger.debug(f"*********** Run End: {self.run_id} ***********")

def run(
self, message: Optional[Union[List, Dict, str]] = None, stream: bool = True, **kwargs: Any
Expand Down
3 changes: 3 additions & 0 deletions phi/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class LLM(BaseModel):
system_prompt: Optional[str] = None
instructions: Optional[List[str]] = None

# State from the run
run_id: Optional[str] = None

model_config = ConfigDict(arbitrary_types_allowed=True)

@property
Expand Down
2 changes: 1 addition & 1 deletion phi/llm/cohere/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from phi.llm.cohere.cohere import Cohere
from phi.llm.cohere.chat import CohereChat
36 changes: 28 additions & 8 deletions phi/llm/cohere/cohere.py → phi/llm/cohere/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
from textwrap import dedent
from typing import Optional, List, Dict, Any


from phi.llm.base import LLM
from phi.llm.message import Message
from phi.tools.function import FunctionCall
from phi.utils.log import logger
from phi.utils.timer import Timer
from phi.utils.tools import (
get_function_call_for_tool_call,
)
from phi.utils.tools import get_function_call_for_tool_call

try:
from cohere import Client as CohereClient
Expand All @@ -27,21 +24,23 @@
raise


class Cohere(LLM):
class CohereChat(LLM):
name: str = "cohere"
model: str = "command-r"
# # -*- Request parameters
# -*- Request parameters
temperature: Optional[float] = None
max_tokens: Optional[int] = None
top_k: Optional[int] = None
top_p: Optional[float] = None
frequency_penalty: Optional[float] = None
presence_penalty: Optional[float] = None
request_params: Optional[Dict[str, Any]] = None
# Use cohere conversation_id to create a persistent conversation
use_conversation_id: bool = True
# -*- Client parameters
api_key: Optional[str] = None
client_params: Optional[Dict[str, Any]] = None
# -*- Provide the client manually
# -*- Provide the Cohere client manually
cohere_client: Optional[CohereClient] = None

@property
Expand All @@ -57,6 +56,8 @@ def client(self) -> CohereClient:
@property
def api_kwargs(self) -> Dict[str, Any]:
_request_params: Dict[str, Any] = {}
if self.use_conversation_id and self.run_id is not None:
_request_params["conversation_id"] = self.run_id
if self.temperature:
_request_params["temperature"] = self.temperature
if self.max_tokens:
Expand Down Expand Up @@ -96,7 +97,26 @@ def get_tools(self) -> Optional[List[Dict[str, Any]]]:

def invoke(self, messages: List[Message], tool_results: Optional[List[ChatRequestToolResultsItem]] = None) -> Chat:
api_kwargs: Dict[str, Any] = self.api_kwargs
api_kwargs["chat_history"] = []
chat_message = None

if not self.use_conversation_id or self.run_id is None:
logger.debug("Providing chat_history to cohere.")
chat_history = []
for m in messages:
if m.role == "system":
api_kwargs["preamble"] = m.content
elif m.role == "user":
if last_user_message is not None:
# Append the previously tracked user message to chat_history before updating it
api_kwargs["chat_history"].append({"role": "USER", "message": last_user_message})
# Update the last user message
last_user_message = m.content
else:
api_kwargs["chat_history"].append({"role": "CHATBOT", "message": m.content or ""})

api_kwargs["chat_history"] = chat_history


user_message: List = []
# Track the last user message to prevent adding it to chat_history
last_user_message = None
Expand Down
2 changes: 1 addition & 1 deletion phi/llm/openai/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class OpenAIChat(LLM):
default_headers: Optional[Any] = None
default_query: Optional[Any] = None
client_params: Optional[Dict[str, Any]] = None
# -*- Provide the OpenAIClient manually
# -*- Provide the OpenAI client manually
openai_client: Optional[OpenAIClient] = None

@property
Expand Down
18 changes: 11 additions & 7 deletions phi/task/llm/llm_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,21 @@ def set_default_llm(self) -> None:

self.llm = OpenAIChat()

def add_response_format_to_llm(self) -> None:
if self.output_model is not None and self.llm is not None:
self.llm.response_format = {"type": "json_object"}

def add_tools_to_llm(self) -> None:
def update_llm(self) -> None:
if self.llm is None:
logger.error(f"Task LLM is None: {self.__class__.__name__}")
return

# Set response_format if it is not set on the llm
if self.output_model is not None and self.llm.response_format is None:
self.llm.response_format = {"type": "json_object"}

# Add tools to the LLM
if self.tools is not None:
for tool in self.tools:
self.llm.add_tool(tool)

# Add default tools to the LLM
if self.use_tools:
if self.read_chat_history_tool and self.memory is not None:
self.llm.add_tool(self.get_chat_history)
Expand All @@ -207,11 +209,13 @@ def add_tools_to_llm(self) -> None:
if self.tool_call_limit is not None and self.tool_call_limit < self.llm.function_call_limit:
self.llm.function_call_limit = self.tool_call_limit

if self.run_id is not None:
self.llm.run_id = self.run_id

def prepare_task(self) -> None:
self.set_task_id()
self.set_default_llm()
self.add_response_format_to_llm()
self.add_tools_to_llm()
self.update_llm()

def get_json_output_prompt(self) -> str:
json_output_prompt = "\nProvide your output as a JSON containing the following fields:"
Expand Down

0 comments on commit 90d9e30

Please sign in to comment.