phidata
phidata copied to clipboard
Returning a string instead of print response
How do you return the answer as a string verse print response:
from phi.assistant.python import PythonAssistant
from phi.file.local.csv import CsvFile
def average_rating():
python_assistant = PythonAssistant(
files=[
CsvFile(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
description="Contains information about movies from IMDB.",
)
],
pip_install=True,
show_tool_calls=True,
)
python_assistant.print_response("What is the average rating of movies?", markdown=True)
@jacobweiss2305 use:
string_response = python_assistant.run("What is the average rating of movies?")
If you want to return the string response in markdown, you'll need to set it on the Assistant:
from phi.assistant.python import PythonAssistant
from phi.file.local.csv import CsvFile
def average_rating():
python_assistant = PythonAssistant(
files=[
CsvFile(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
description="Contains information about movies from IMDB.",
)
],
pip_install=True,
show_tool_calls=True,
markdown=True,
)
response = python_assistant.run("What is the average rating of movies?")
@jacobweiss2305 i think you're onto something very cool here, I created a cookbook which might help you get more out of this. Try this code:
from phi.assistant.python import PythonAssistant
from phi.file.local.csv import CsvFile
from rich.pretty import pprint
from pydantic import BaseModel, Field
class AssistantResponse(BaseModel):
result: str = Field(..., description="The result of the users question.")
def average_rating() -> AssistantResponse:
python_assistant = PythonAssistant(
files=[
CsvFile(
path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
description="Contains information about movies from IMDB.",
)
],
instructions=[
"Only provide the result, do not need to provide any additional information.",
],
# This will make sure the output is of this Assistant is an object of the `AssistantResponse` class
output_model=AssistantResponse,
# This will allow the Assistant to directly run python code, risky but fun
run_code=True,
# This will allow the Assistant to save python code before running, less risky and you have a log of what was run
save_and_run=False,
# Uncomment the following line to return result in markdown
# markdown=True,
# Uncomment the following line to let the assistant install python packages
# pip_install=True,
# Uncomment the following line to show debug logs
# debug_mode=True,
)
response: AssistantResponse = python_assistant.run("What is the average rating of movies?") # type: ignore
return response
response: AssistantResponse = average_rating()
pprint(response)
# Output:
# AssistantResponse(result='6.7232')
@ashpreetbedi , thank you!!