-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## OpenRouter | ||
|
||
> Note: Fork and clone this repository if needed | ||
1. Create a virtual environment | ||
|
||
```shell | ||
python3 -m venv ~/.venvs/aienv | ||
source ~/.venvs/aienv/bin/activate | ||
``` | ||
|
||
2. Install libraries | ||
|
||
```shell | ||
pip install -U openai phidata | ||
``` | ||
|
||
3. Export `OPENROUTER_API_KEY` | ||
|
||
```shell | ||
export OPENROUTER_API_KEY=*** | ||
``` | ||
|
||
4. Test OpenRouter Assistant | ||
|
||
- Streaming | ||
|
||
```shell | ||
python cookbook/llms/openrouter/assistant.py | ||
``` | ||
|
||
- Without Streaming | ||
|
||
```shell | ||
python cookbook/llms/openrouter/assistant_stream_off.py | ||
``` | ||
|
||
5. Test Structured output | ||
|
||
```shell | ||
python cookbook/llms/openrouter/pydantic_output.py | ||
``` | ||
|
||
6. Test function calling | ||
|
||
```shell | ||
python cookbook/llms/openrouter/tool_call.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from phi.assistant import Assistant | ||
from phi.llm.openrouter import OpenRouter | ||
|
||
assistant = Assistant( | ||
llm=OpenRouter(model="mistralai/mistral-7b-instruct:free"), | ||
description="You help people with their health and fitness goals.", | ||
) | ||
assistant.print_response("Share a 2 sentence quick and healthy breakfast recipe.", markdown=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from phi.assistant import Assistant | ||
from phi.llm.openrouter import OpenRouter | ||
|
||
assistant = Assistant( | ||
llm=OpenRouter(model="mistralai/mistral-7b-instruct:free"), | ||
description="You help people with their health and fitness goals.", | ||
) | ||
assistant.print_response("Share a 2 sentence quick and healthy breakfast recipe.", markdown=True, stream=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import List | ||
from pydantic import BaseModel, Field | ||
from rich.pretty import pprint | ||
from phi.assistant import Assistant | ||
from phi.llm.openrouter import OpenRouter | ||
|
||
|
||
class MovieScript(BaseModel): | ||
setting: str = Field(..., description="Provide a nice setting for a blockbuster movie.") | ||
ending: str = Field(..., description="Ending of the movie. If not available, provide a happy ending.") | ||
genre: str = Field( | ||
..., description="Genre of the movie. If not available, select action, thriller or romantic comedy." | ||
) | ||
name: str = Field(..., description="Give a name to this movie") | ||
characters: List[str] = Field(..., description="Name of characters for this movie.") | ||
storyline: str = Field(..., description="3 sentence storyline for the movie. Make it exciting!") | ||
|
||
|
||
movie_assistant = Assistant( | ||
llm=OpenRouter(), | ||
description="You help people write movie ideas.", | ||
output_model=MovieScript, | ||
) | ||
|
||
pprint(movie_assistant.run("New York")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from phi.assistant import Assistant | ||
from phi.llm.openrouter import OpenRouter | ||
from phi.tools.duckduckgo import DuckDuckGo | ||
|
||
assistant = Assistant( | ||
llm=OpenRouter(model="openai/gpt-3.5-turbo"), tools=[DuckDuckGo()], show_tool_calls=True, debug_mode=True | ||
) | ||
assistant.print_response("Whats happening in France?", markdown=True, stream=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from phi.llm.openrouter.openrouter import OpenRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from os import getenv | ||
from typing import Optional | ||
|
||
from phi.llm.openai.like import OpenAILike | ||
|
||
|
||
class OpenRouter(OpenAILike): | ||
name: str = "OpenRouter" | ||
model: str = "mistralai/mistral-7b-instruct:free" | ||
api_key: Optional[str] = getenv("OPENROUTER_API_KEY") | ||
base_url: str = "https://openrouter.ai/api/v1" |