Skip to content

Commit

Permalink
Context injection
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Nov 25, 2024
1 parent 7432f08 commit 09faf86
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
9 changes: 0 additions & 9 deletions cookbook/agents/24_agent_with_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@


def get_top_hackernews_stories(num_stories: int = 5) -> str:
"""Use this function to get top stories from Hacker News.
Args:
num_stories (int): Number of stories to return. Defaults to 10.
Returns:
str: JSON string of top stories.
"""
# Fetch top story IDs
response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
story_ids = response.json()
Expand All @@ -29,7 +21,6 @@ def get_top_hackernews_stories(num_stories: int = 5) -> str:


agent = Agent(
tools=[get_top_hackernews_stories],
context={
"name": "John Doe",
"top_stories": lambda: get_top_hackernews_stories(3),
Expand Down
14 changes: 14 additions & 0 deletions cookbook/agents/25_system_prompt_via_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from phi.agent import Agent


def get_system_prompt(agent: Agent) -> str:
return f"You are {agent.name}!"


agent = Agent(
name="AgentX",
system_prompt=get_system_prompt,
markdown=True,
show_tool_calls=True,
)
agent.print_response("Who are you?", stream=True)
16 changes: 16 additions & 0 deletions cookbook/agents/26_instructions_via_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List

from phi.agent import Agent


def get_instructions(agent: Agent) -> List[str]:
return ["Talk in haiku's!", "Use poetry to answer questions."]


agent = Agent(
name="AgentX",
instructions=get_instructions,
markdown=True,
show_tool_calls=True,
)
agent.print_response("Who are you?", stream=True)
4 changes: 1 addition & 3 deletions phi/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ def update_model(self) -> None:

def _resolve_context(self) -> None:
from inspect import signature
from functools import partial

logger.debug("Resolving context")
if self.context is not None:
Expand All @@ -504,8 +503,7 @@ def _resolve_context(self) -> None:
sig = signature(ctx_value)
resolved_ctx_value = None
if "agent" in sig.parameters:
ctx_value_partial = partial(ctx_value, agent=self)
resolved_ctx_value = ctx_value_partial()
resolved_ctx_value = ctx_value(agent=self)
else:
resolved_ctx_value = ctx_value()
if resolved_ctx_value is not None:
Expand Down
5 changes: 4 additions & 1 deletion phi/model/google/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ def _format_functions(self, params: Dict[str, Any]) -> Dict[str, Any]:
return formatted_params

def add_tool(
self, tool: Union["Tool", "Toolkit", Callable, dict, "Function"], strict: bool = False, agent: Optional[Any] = None
self,
tool: Union["Tool", "Toolkit", Callable, dict, "Function"],
strict: bool = False,
agent: Optional[Any] = None,
) -> None:
"""
Adds tools to the model.
Expand Down

0 comments on commit 09faf86

Please sign in to comment.