Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/phidatahq/phidata
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Nov 14, 2024
2 parents 4a42686 + 342d882 commit 765962c
Show file tree
Hide file tree
Showing 10 changed files with 537 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ MovieScript(

</details>

### Checkout the [cookbook](https://github.com/phidatahq/phidata/tree/main/cookbook) for more examples.
### Check out the [cookbook](https://github.com/phidatahq/phidata/tree/main/cookbook) for more examples.

## Contributions

Expand Down
11 changes: 7 additions & 4 deletions cookbook/assistants/examples/pdf/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ def pdf_assistant(new: bool = False, user: str = "user"):
print(f"Continuing Run: {run_id}\n")

while True:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in ("exit", "bye"):
break
assistant.print_response(message, markdown=True)
try:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in ("exit", "bye"):
break
assistant.print_response(message, markdown=True)
except Exception as e:
print(f"[red]Error: {e}[/red]") # Added error handling


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion cookbook/assistants/integrations/singlestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install -U pymysql sqlalchemy pypdf openai phidata

- For SingleStore

> Note: If using a shared tier, please provide a certificate file for SSL connection [Read more](https://docs.singlestore.com/cloud/connect-to-your-workspace/connect-with-mysql/connect-with-mysql-client/connect-to-singlestore-helios-using-tls-ssl/)
> Note: If using a shared tier, please provide a certificate file for SSL connection [Read more](https://docs.singlestore.com/cloud/connect-to-singlestore/connect-with-mysql/connect-with-mysql-client/connect-to-singlestore-helios-using-tls-ssl/)
```shell
export SINGLESTORE_HOST="host"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pip install -r cookbook/integrations/singlestore/ai_apps/requirements.txt

- For SingleStore

> Note: If using a shared tier, please provide a certificate file for SSL connection [Read more](https://docs.singlestore.com/cloud/connect-to-your-workspace/connect-with-mysql/connect-with-mysql-client/connect-to-singlestore-helios-using-tls-ssl/)
> Note: If using a shared tier, please provide a certificate file for SSL connection [Read more](https://docs.singlestore.com/cloud/connect-to-singlestore/connect-with-mysql/connect-with-mysql-client/connect-to-singlestore-helios-using-tls-ssl/)
```shell
export SINGLESTORE_HOST="host"
Expand Down
2 changes: 1 addition & 1 deletion cookbook/integrations/singlestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install -U pymysql sqlalchemy pypdf openai phidata

- For SingleStore

> Note: If using a shared tier, please provide a certificate file for SSL connection [Read more](https://docs.singlestore.com/cloud/connect-to-your-workspace/connect-with-mysql/connect-with-mysql-client/connect-to-singlestore-helios-using-tls-ssl/)
> Note: If using a shared tier, please provide a certificate file for SSL connection [Read more](https://docs.singlestore.com/cloud/connect-to-singlestore/connect-with-mysql/connect-with-mysql-client/connect-to-singlestore-helios-using-tls-ssl/)
```shell
export SINGLESTORE_HOST="host"
Expand Down
44 changes: 44 additions & 0 deletions cookbook/tools/calcom_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from datetime import datetime

from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.calcom import CalCom

"""
Example showing how to use the Cal.com Tools with Phi.
Requirements:
- Cal.com API key (get from cal.com/settings/developer/api-keys)
- Event Type ID from Cal.com
- pip install requests pytz
Usage:
- Set the following environment variables:
export CALCOM_API_KEY="your_api_key"
export CALCOM_EVENT_TYPE_ID="your_event_type_id"
- Or provide them when creating the CalComTools instance
"""

INSTRUCTONS = f"""You're scheduing assistant. Today is {datetime.now()}.
You can help users by:
- Finding available time slots
- Creating new bookings
- Managing existing bookings (view, reschedule, cancel)
- Getting booking details
- IMPORTANT: In case of rescheduling or cancelling booking, call the get_upcoming_bookings function to get the booking uid. check available slots before making a booking for given time
Always confirm important details before making bookings or changes.
"""


agent = Agent(
name="Calendar Assistant",
instructions=[INSTRUCTONS],
model=OpenAIChat(id="gpt-4"),
tools=[CalCom(user_timezone="America/New_York")],
show_tool_calls=True,
markdown=True,
)

# Example usage
agent.print_response("What are my bookings for tomorrow?")
42 changes: 42 additions & 0 deletions cookbook/tools/twilio_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.twilio import TwilioTools

"""
Example showing how to use the Twilio Tools with Phi.
Requirements:
- Twilio Account SID and Auth Token (get from console.twilio.com)
- A Twilio phone number
- pip install twilio
Usage:
- Set the following environment variables:
export TWILIO_ACCOUNT_SID="your_account_sid"
export TWILIO_AUTH_TOKEN="your_auth_token"
- Or provide them when creating the TwilioTools instance
"""


agent = Agent(
name="Twilio Agent",
instructions=[
"""You can help users by:
- Sending SMS messages
- Checking message history
- getting call details
"""
],
model=OpenAIChat(id="gpt-4o"),
tools=[TwilioTools()],
show_tool_calls=True,
markdown=True,
)

sender_phone_number = "+1234567890"
receiver_phone_number = "+1234567890"

agent.print_response(
f"Can you send an SMS saying 'Your package has arrived' to {receiver_phone_number} from {sender_phone_number}?"
)
21 changes: 18 additions & 3 deletions phi/model/google/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ def response(self, messages: List[Message]) -> ModelResponse:
if assistant_message.content is not None:
model_response.content = assistant_message.get_content_string()

# -*- Remove parts from messages
for m in messages:
if hasattr(m, "parts"):
m.parts = None

logger.debug("---------- Gemini Response End ----------")
return model_response

Expand Down Expand Up @@ -551,13 +556,14 @@ def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]:
for response in self.invoke_stream(messages=messages):
message_data.response_block = response.candidates[0].content
message_data.response_role = message_data.response_block.role
message_data.response_parts = message_data.response_block.parts
if message_data.response_block.parts:
message_data.response_parts = message_data.response_block.parts

if message_data.response_parts is not None:
for part in message_data.response_parts:
part_dict = type(part).to_dict(part)

# Yield text if present
# -*- Yield text if present
if "text" in part_dict:
text = part_dict.get("text")
yield ModelResponse(content=text)
Expand All @@ -567,7 +573,10 @@ def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]:
stream_usage_data.time_to_first_token = response_timer.elapsed
logger.debug(f"Time to first token: {stream_usage_data.time_to_first_token:.4f}s")

# Parse function calls
# -*- Skip function calls if there are no parts
if not message_data.response_block.parts and message_data.response_parts:
continue
# -*- Parse function calls
if "function_call" in part_dict:
message_data.response_tool_calls.append(
{
Expand Down Expand Up @@ -610,4 +619,10 @@ def response_stream(self, messages: List[Message]) -> Iterator[ModelResponse]:
if assistant_message.tool_calls is not None and len(assistant_message.tool_calls) > 0 and self.run_tools:
yield from self._handle_stream_tool_calls(assistant_message, messages)
yield from self.response_stream(messages=messages)

# -*- Remove parts from messages
for m in messages:
if hasattr(m, "parts"):
m.parts = None

logger.debug("---------- Gemini Response End ----------")
Loading

0 comments on commit 765962c

Please sign in to comment.