Skip to content

Commit

Permalink
feat(agents-api): bug fixes for the prompt step and types of tool cal…
Browse files Browse the repository at this point in the history
…l fixed
  • Loading branch information
Vedantsahai18 committed Oct 31, 2024
1 parent 74982fa commit 2d83db3
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 24 deletions.
5 changes: 3 additions & 2 deletions agents-api/agents_api/activities/execute_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ async def execute_system(
return create_session_query(**arguments)
elif system.operation == "update":
return update_session_query(**arguments)
elif system.operation == "delete":
return update_session_query(**arguments)
# FIXME: @hamada needs to be fixed
# elif system.operation == "delete":
# return update_session_query(**arguments)
elif system.operation == "delete":
return delete_session_query(**arguments)
# TASKS
Expand Down
45 changes: 29 additions & 16 deletions agents-api/agents_api/activities/task_steps/prompt_step.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from anthropic import Anthropic # Import Anthropic client
from anthropic import AsyncAnthropic, Anthropic # Import AsyncAnthropic client
from beartype import beartype
from temporalio import activity
from temporalio.exceptions import ApplicationError
Expand All @@ -9,6 +9,7 @@
from ...clients import (
litellm, # We dont directly import `acompletion` so we can mock it
)
from litellm.utils import CustomStreamWrapper, ModelResponse
from ...common.protocol.tasks import StepContext, StepOutcome
from ...common.storage_handler import auto_blob_store
from ...common.utils.template import render_template
Expand All @@ -20,7 +21,7 @@

# FIXME: This shouldn't be here.
def format_agent_tool(tool: Tool) -> dict:
if tool.function:
if tool.type == "function":
return {
"type": "function",
"function": {
Expand All @@ -29,20 +30,20 @@ def format_agent_tool(tool: Tool) -> dict:
"parameters": tool.function.parameters,
},
}
elif tool.computer_20241022:
elif tool.type == "computer_20241022":
return {
"type": tool.type,
"name": tool.name,
"display_width_px": tool.display_width_px,
"display_height_px": tool.display_height_px,
"display_number": tool.display_number,
"display_width_px": tool.spec['display_width_px'],
"display_height_px": tool.spec['display_height_px'],
"display_number": tool.spec['display_number'],
}
elif tool.bash_20241022:
elif tool.type == "bash_20241022":
return {
"type": tool.type,
"name": tool.name,
}
elif tool.text_editor_20241022:
elif tool.type == "text_editor_20241022":
return {
"type": tool.type,
"name": tool.name,
Expand All @@ -51,7 +52,6 @@ def format_agent_tool(tool: Tool) -> dict:
else:
return {}


@activity.defn
@auto_blob_store
@beartype
Expand Down Expand Up @@ -86,7 +86,7 @@ async def prompt_step(context: StepContext) -> StepOutcome:
sort_by="created_at",
direction="desc",
)

# grab the tools from context.current_step.tools and then append it to the agent_tools
if context.current_step.settings:
passed_settings: dict = context.current_step.settings.model_dump(
exclude_unset=True
Expand All @@ -99,25 +99,38 @@ async def prompt_step(context: StepContext) -> StepOutcome:
prompt = [{"role": "user", "content": prompt}]

# Format agent_tools for litellm
# Initialize the formatted_agent_tools with context tools
task_tools = context.tools
formatted_agent_tools = [
format_agent_tool(tool) for tool in agent_tools if format_agent_tool(tool)
format_agent_tool(tool) for tool in task_tools
]
# Add agent_tools if they are not already in formatted_agent_tools
for agent_tool in agent_tools:
if format_agent_tool(agent_tool) and format_agent_tool(agent_tool) not in formatted_agent_tools:
formatted_agent_tools.append(format_agent_tool(agent_tool))

# Check if the model is Anthropic
if "claude-3.5-sonnet-20241022" == agent_model.lower():
if (agent_model.lower().startswith("claude-3.5")
and any(
tool["type"] in ["computer_20241022", "bash_20241022", "text_editor_20241022"]
for tool in formatted_agent_tools)
):
# Retrieve the API key from the environment variable
betas = [COMPUTER_USE_BETA_FLAG]
# Use Anthropic API directly
client = Anthropic(api_key=anthropic_api_key)

client = AsyncAnthropic(api_key=anthropic_api_key)
new_prompt = [{"role": "user", "content": prompt[0]["content"]}]
# Claude Response
response = await client.beta.messages.create(
model=agent_model,
messages=prompt,
model="claude-3-5-sonnet-20241022",
messages=new_prompt,
tools=formatted_agent_tools,
max_tokens=1024,
betas=betas,
)
print("-" * 100)
# print("Model Response: ", model_response)
print("Response: ", response)

return StepOutcome(
output=response.model_dump()
Expand Down
51 changes: 51 additions & 0 deletions agents-api/agents_api/autogen/Tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,18 @@ class CreateToolRequest(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down Expand Up @@ -953,6 +965,21 @@ class PatchToolRequest(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: (
Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
| None
) = None
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down Expand Up @@ -1380,6 +1407,18 @@ class Tool(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down Expand Up @@ -1457,6 +1496,18 @@ class UpdateToolRequest(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down
11 changes: 5 additions & 6 deletions agents-api/agents_api/common/protocol/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,12 @@ def task_to_spec(
for tool in task.tools:
tool_spec = getattr(tool, tool.type)

tools.append(
TaskToolDef(
type=tool.type,
spec=tool_spec.model_dump(),
**tool.model_dump(exclude={"type"}),
)
tool_obj = dict(
type=tool.type,
spec=tool_spec.model_dump(),
**tool.model_dump(exclude={"type"}),
)
tools.append(TaskToolDef(**tool_obj))

workflows = [Workflow(name="main", steps=task_data.pop("main"))]

Expand Down
51 changes: 51 additions & 0 deletions integrations-service/integrations/autogen/Tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,18 @@ class CreateToolRequest(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down Expand Up @@ -953,6 +965,21 @@ class PatchToolRequest(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: (
Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
| None
) = None
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down Expand Up @@ -1380,6 +1407,18 @@ class Tool(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down Expand Up @@ -1457,6 +1496,18 @@ class UpdateToolRequest(BaseModel):
"""
Name of the tool (must be unique for this agent and a valid python identifier string )
"""
type: Literal[
"function",
"integration",
"system",
"api_call",
"computer_20241022",
"text_editor_20241022",
"bash_20241022",
]
"""
Type of the tool
"""
description: str | None = None
"""
Description of the tool
Expand Down
3 changes: 3 additions & 0 deletions typespec/tools/models.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ model Tool {
/** Name of the tool (must be unique for this agent and a valid python identifier string )*/
name: validPythonIdentifier;

/** Type of the tool */
type: ToolType;

/** Description of the tool */
description?: string;

Expand Down
19 changes: 19 additions & 0 deletions typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6656,11 +6656,16 @@ components:
type: object
required:
- name
- type
properties:
name:
allOf:
- $ref: '#/components/schemas/Common.validPythonIdentifier'
description: Name of the tool (must be unique for this agent and a valid python identifier string )
type:
allOf:
- $ref: '#/components/schemas/Tools.ToolType'
description: Type of the tool
description:
type: string
description: Description of the tool
Expand Down Expand Up @@ -6885,6 +6890,10 @@ components:
allOf:
- $ref: '#/components/schemas/Common.validPythonIdentifier'
description: Name of the tool (must be unique for this agent and a valid python identifier string )
type:
allOf:
- $ref: '#/components/schemas/Tools.ToolType'
description: Type of the tool
description:
type: string
description: Description of the tool
Expand Down Expand Up @@ -7291,6 +7300,7 @@ components:
type: object
required:
- name
- type
- created_at
- updated_at
- id
Expand All @@ -7299,6 +7309,10 @@ components:
allOf:
- $ref: '#/components/schemas/Common.validPythonIdentifier'
description: Name of the tool (must be unique for this agent and a valid python identifier string )
type:
allOf:
- $ref: '#/components/schemas/Tools.ToolType'
description: Type of the tool
description:
type: string
description: Description of the tool
Expand Down Expand Up @@ -7380,11 +7394,16 @@ components:
type: object
required:
- name
- type
properties:
name:
allOf:
- $ref: '#/components/schemas/Common.validPythonIdentifier'
description: Name of the tool (must be unique for this agent and a valid python identifier string )
type:
allOf:
- $ref: '#/components/schemas/Tools.ToolType'
description: Type of the tool
description:
type: string
description: Description of the tool
Expand Down

0 comments on commit 2d83db3

Please sign in to comment.