Skip to content

Commit

Permalink
Feat: Add together as a provider (#1182)
Browse files Browse the repository at this point in the history
* Feat: add anyscale as a provider

* fix linting

* Feat: Add together as a provider

* fix linting

* updated cookbook

* resolving comments

* matching the cookbook with openai

* update cookbooks to match openai

* update readme

* fix readme

* linting fix

* together model

* fix linting

* change models

* remove anyscale
  • Loading branch information
manthanguptaa authored Oct 4, 2024
1 parent 6c8f406 commit 32fc832
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cookbook/providers/together/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Together Cookbook

> Note: Fork and clone this repository if needed
### 1. Create and activate a virtual environment

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

### 2. Export your `TOGETHER_API_KEY`

```shell
export TOGETHER_API_KEY=***
```

### 3. Install libraries

```shell
pip install -U together openai duckduckgo-search duckdb yfinance phidata
```

### 4. Run Agent without Tools

- Streaming on

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

- Streaming off

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

### 5. Run Agent with Tools

- Yahoo Finance with streaming on

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

- Yahoo Finance without streaming

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

- Finance Agent

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

- Data Analyst

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

- DuckDuckGo Search
```shell
python cookbook/providers/together/web_search.py
```

### 6. Run Agent that returns structured output

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

19 changes: 19 additions & 0 deletions cookbook/providers/together/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.together import Together
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
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 on the terminal
agent.print_response("What is the stock price of NVDA and TSLA")
22 changes: 22 additions & 0 deletions cookbook/providers/together/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.together import Together
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
tools=[YFinanceTools(stock_price=True)],
instructions=["Use tables where possible."],
show_tool_calls=True,
markdown=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 on the terminal
agent.print_response("What is the stock price of NVDA and TSLA", stream=True)
11 changes: 11 additions & 0 deletions cookbook/providers/together/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.together import Together

agent = Agent(model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"), 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/together/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.together import Together

agent = Agent(model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"), 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/together/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.together import Together
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=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
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/together/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.together import Together
from phi.tools.yfinance import YFinanceTools

agent = Agent(
model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
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)
32 changes: 32 additions & 0 deletions cookbook/providers/together/structured_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.together import Together


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
json_mode_agent = Agent(
model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
description="You write movie scripts.",
response_model=MovieScript,
)

# Get the response in a variable
# json_mode_response: RunResponse = json_mode_agent.run("New York")
# pprint(json_mode_response.content)
# structured_output_response: RunResponse = structured_output_agent.run("New York")
# pprint(structured_output_response.content)

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

from phi.agent import Agent
from phi.model.together import Together
from phi.tools.duckduckgo import DuckDuckGo

agent = Agent(
model=Together(id="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
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/together/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from phi.model.together.together import Together
Loading

0 comments on commit 32fc832

Please sign in to comment.