Skip to content

Commit

Permalink
add-openrouter-support-phi-663
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Apr 22, 2024
1 parent c335f8d commit 5769fda
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cookbook/llms/openrouter/README.md
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
```
8 changes: 8 additions & 0 deletions cookbook/llms/openrouter/assistant.py
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)
8 changes: 8 additions & 0 deletions cookbook/llms/openrouter/assistant_stream_off.py
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)
25 changes: 25 additions & 0 deletions cookbook/llms/openrouter/pydantic_output.py
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"))
8 changes: 8 additions & 0 deletions cookbook/llms/openrouter/tool_call.py
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)
1 change: 1 addition & 0 deletions phi/llm/openrouter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from phi.llm.openrouter.openrouter import OpenRouter
11 changes: 11 additions & 0 deletions phi/llm/openrouter/openrouter.py
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"

0 comments on commit 5769fda

Please sign in to comment.