Skip to content

Commit

Permalink
Merge pull request #1443 from phidatahq/ollamatools-phi-1991
Browse files Browse the repository at this point in the history
ollamatools-phi-1991
  • Loading branch information
ashpreetbedi authored Nov 14, 2024
2 parents d082ffc + 14d5a10 commit c3f9639
Show file tree
Hide file tree
Showing 15 changed files with 670 additions and 0 deletions.
90 changes: 90 additions & 0 deletions cookbook/providers/ollama_tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# OllamaTools Cookbook

> Note: Fork and clone this repository if needed
### 1. [Install](https://github.com/ollama/ollama?tab=readme-ov-file#macos) ollama and run models

Run your chat model

```shell
ollama run llama3.2
```

Message `/bye` to exit the chat model

### 2. Create and activate a virtual environment

```shell
python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate
```

### 3. Install libraries

```shell
pip install -U ollama duckduckgo-search duckdb yfinance phidata
```

### 4. Run Agent without Tools

- Streaming on

```shell
python cookbook/providers/ollama_tools/basic_stream.py
```

- Streaming off

```shell
python cookbook/providers/ollama_tools/basic.py
```

### 5. Run Agent with Tools

- Yahoo Finance with streaming on

```shell
python cookbook/providers/ollama_tools/agent_stream.py
```

- Yahoo Finance without streaming

```shell
python cookbook/providers/ollama_tools/agent.py
```

- Finance Agent

```shell
python cookbook/providers/ollama_tools/finance_agent.py
```

- Data Analyst

```shell
python cookbook/providers/ollama_tools/data_analyst.py
```

- Web Search

```shell
python cookbook/providers/ollama_tools/web_search.py
```

### 6. Run Agent that returns structured output

```shell
python cookbook/providers/ollama_tools/structured_output.py
```

### 7. Run Agent that uses storage

```shell
python cookbook/providers/ollama_tools/storage.py
```

### 8. Run Agent that uses knowledge

```shell
python cookbook/providers/ollama_tools/knowledge.py
```
Empty file.
19 changes: 19 additions & 0 deletions cookbook/providers/ollama_tools/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Run `pip install yfinance` to install dependencies."""

from phi.agent import Agent, RunResponse # noqa
from phi.model.ollama import OllamaTools
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=OllamaTools(id="llama3.2"),
tools=[YFinanceTools(stock_price=True)],
show_tool_calls=True,
markdown=True,
)

# Get the response in a variable
# run: RunResponse = agent.run("What is the stock price of NVDA and TSLA")
# print(run.content)

# Print the response in the terminal
agent.print_response("What is the stock price of NVDA and TSLA")
22 changes: 22 additions & 0 deletions cookbook/providers/ollama_tools/agent_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Run `pip install yfinance` to install dependencies."""

from typing import Iterator # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.ollama import OllamaTools
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=OllamaTools(id="llama3.2"),
tools=[YFinanceTools(stock_price=True)],
instructions=["Use tables where possible."],
markdown=True,
show_tool_calls=True,
)

# Get the response in a variable
# run_response: Iterator[RunResponse] = agent.run("What is the stock price of NVDA and TSLA", stream=True)
# for chunk in run_response:
# print(chunk.content)

# Print the response in the terminal
agent.print_response("What is the stock price of NVDA and TSLA", stream=True)
33 changes: 33 additions & 0 deletions cookbook/providers/ollama_tools/agent_team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from phi.agent import Agent
from phi.model.ollama import OllamaTools
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools

web_agent = Agent(
name="Web Agent",
role="Search the web for information",
model=OllamaTools(id="llama3.2"),
tools=[DuckDuckGo()],
instructions=["Always include sources"],
show_tool_calls=True,
markdown=True,
)

finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
model=OllamaTools(id="llama3.2"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
instructions=["Use tables to display data"],
show_tool_calls=True,
markdown=True,
)

agent_team = Agent(
team=[web_agent, finance_agent],
instructions=["Always include sources", "Use tables to display data"],
show_tool_calls=True,
markdown=True,
)

agent_team.print_response("Summarize analyst recommendations and share the latest news for NVDA", stream=True)
11 changes: 11 additions & 0 deletions cookbook/providers/ollama_tools/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from phi.agent import Agent, RunResponse # noqa
from phi.model.ollama import OllamaTools

agent = Agent(model=OllamaTools(id="llama3.2"), markdown=True)

# Get the response in a variable
# run: RunResponse = agent.run("Share a 2 sentence horror story")
# print(run.content)

# Print the response in the terminal
agent.print_response("Share a 2 sentence horror story")
13 changes: 13 additions & 0 deletions cookbook/providers/ollama_tools/basic_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Iterator # noqa
from phi.agent import Agent, RunResponse # noqa
from phi.model.ollama import OllamaTools

agent = Agent(model=OllamaTools(id="llama3.2"), markdown=True)

# Get the response in a variable
# run_response: Iterator[RunResponse] = agent.run("Share a 2 sentence horror story", stream=True)
# for chunk in run_response:
# print(chunk.content)

# Print the response in the terminal
agent.print_response("Share a 2 sentence horror story", stream=True)
23 changes: 23 additions & 0 deletions cookbook/providers/ollama_tools/data_analyst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Run `pip install duckdb` to install dependencies."""

from textwrap import dedent
from phi.agent import Agent
from phi.model.ollama import OllamaTools
from phi.tools.duckdb import DuckDbTools

duckdb_tools = DuckDbTools(create_tables=False, export_tables=False, summarize_tables=False)
duckdb_tools.create_table_from_path(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv", table="movies"
)

agent = Agent(
model=OllamaTools(id="llama3.2"),
tools=[duckdb_tools],
markdown=True,
show_tool_calls=True,
additional_context=dedent("""\
You have access to the following tables:
- movies: contains information about movies from IMDB.
"""),
)
agent.print_response("What is the average rating of movies?", stream=False)
17 changes: 17 additions & 0 deletions cookbook/providers/ollama_tools/finance_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Run `pip install yfinance` to install dependencies."""

from phi.agent import Agent
from phi.model.ollama import OllamaTools
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=OllamaTools(id="llama3.2"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, stock_fundamentals=True)],
show_tool_calls=True,
description="You are an investment analyst that researches stocks and helps users make informed decisions.",
instructions=["Use tables to display data where possible."],
markdown=True,
)

# agent.print_response("Share the NVDA stock price and analyst recommendations", stream=True)
agent.print_response("Summarize fundamentals for TSLA", stream=True)
22 changes: 22 additions & 0 deletions cookbook/providers/ollama_tools/knowledge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Run `pip install duckduckgo-search sqlalchemy pgvector pypdf openai ollama` to install dependencies."""

from phi.agent import Agent
from phi.model.ollama import OllamaTools
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector(table_name="recipes", db_url=db_url),
)
knowledge_base.load(recreate=False) # Comment out after first run

agent = Agent(
model=OllamaTools(id="llama3.2"),
knowledge_base=knowledge_base,
use_tools=True,
show_tool_calls=True,
)
agent.print_response("How to make Thai curry?", markdown=True)
17 changes: 17 additions & 0 deletions cookbook/providers/ollama_tools/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Run `pip install duckduckgo-search sqlalchemy ollama` to install dependencies."""

from phi.agent import Agent
from phi.model.ollama import OllamaTools
from phi.tools.duckduckgo import DuckDuckGo
from phi.storage.agent.postgres import PgAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

agent = Agent(
model=OllamaTools(id="llama3.2"),
storage=PgAgentStorage(table_name="agent_sessions", db_url=db_url),
tools=[DuckDuckGo()],
add_history_to_messages=True,
)
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")
30 changes: 30 additions & 0 deletions cookbook/providers/ollama_tools/structured_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List
from rich.pretty import pprint # noqa
from pydantic import BaseModel, Field
from phi.agent import Agent, RunResponse # noqa
from phi.model.ollama import OllamaTools


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!")


# Agent that uses JSON mode
movie_agent = Agent(
model=OllamaTools(id="llama3.2"),
description="You write movie scripts.",
response_model=MovieScript,
)

# Get the response in a variable
# run: RunResponse = movie_agent.run("New York")
# pprint(run.content)

movie_agent.print_response("New York")
8 changes: 8 additions & 0 deletions cookbook/providers/ollama_tools/web_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Run `pip install duckduckgo-search` to install dependencies."""

from phi.agent import Agent
from phi.model.ollama import OllamaTools
from phi.tools.duckduckgo import DuckDuckGo

agent = Agent(model=OllamaTools(id="llama3.2"), tools=[DuckDuckGo()], show_tool_calls=True, markdown=True)
agent.print_response("Whats happening in France?", stream=True)
1 change: 1 addition & 0 deletions phi/model/ollama/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from phi.model.ollama.chat import Ollama
from phi.model.ollama.hermes import Hermes
from phi.model.ollama.tools import OllamaTools
Loading

0 comments on commit c3f9639

Please sign in to comment.