diff --git a/proxy/__pycache__/serve.cpython-310.pyc b/proxy/__pycache__/serve.cpython-310.pyc index d3f6587..ca4e2fc 100644 Binary files a/proxy/__pycache__/serve.cpython-310.pyc and b/proxy/__pycache__/serve.cpython-310.pyc differ diff --git a/proxy/requirements.txt b/proxy/requirements.txt index 6be2e1e..193dbfb 100644 --- a/proxy/requirements.txt +++ b/proxy/requirements.txt @@ -2,4 +2,6 @@ fastapi==0.115.7 httpx==0.28.1 uvicorn==0.34.0 invariant-sdk -starlette-compress==1.4.0 \ No newline at end of file +starlette-compress==1.4.0 +tavily-python +anthropic \ No newline at end of file diff --git a/proxy/routes/__pycache__/anthropic.cpython-310.pyc b/proxy/routes/__pycache__/anthropic.cpython-310.pyc index d02d4b7..a8453a1 100644 Binary files a/proxy/routes/__pycache__/anthropic.cpython-310.pyc and b/proxy/routes/__pycache__/anthropic.cpython-310.pyc differ diff --git a/proxy/routes/__pycache__/open_ai.cpython-310.pyc b/proxy/routes/__pycache__/open_ai.cpython-310.pyc index f486d96..ec37170 100644 Binary files a/proxy/routes/__pycache__/open_ai.cpython-310.pyc and b/proxy/routes/__pycache__/open_ai.cpython-310.pyc differ diff --git a/proxy/tests/claude_weather_agent b/proxy/tests/claude_weather_agent new file mode 100644 index 0000000..ba26e1a --- /dev/null +++ b/proxy/tests/claude_weather_agent @@ -0,0 +1,117 @@ +from anthropic import Anthropic +from typing import Dict, Optional, List +import os +from tavily import TavilyClient +import anthropic +from httpx import Client +tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API_KEY")) + +class WeatherAgent: + def __init__(self, api_key: str): + # self.client = Anthropic(api_key=api_key) + dataset_name = "claude_weather_agent_test7" + self.client = anthropic.Anthropic( + http_client=Client( + headers={ + "Invariant-Authorization": "Bearer inv-ff9cb8955c73e3d0afef86a5cef1ee773b1b349d9ed40886c78ef99b8d3dbc5a" + }, + ), + base_url=f"http://localhost/api/v1/proxy/{dataset_name}/anthropic", + ) + self.example_function = { + "name": "get_weather", + "description": "Get the current weather in a given location", + "input_schema": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"], + "description": "The unit of temperature, either \"celsius\" or \"fahrenheit\"" + } + }, + "required": ["location"] + } + } + + + self.system_prompt = """You are an assistant that can perform weather searches using function calls. + When a user asks for weather information, respond with a JSON object specifying + the function name `get_weather` and the arguments latitude and longitude are needed.""" + + def parse_weather_query(self, user_query: str) -> Dict: + """ + Parse user query to extract weather-related parameters using Claude. + """ + messages = [ + { + "role": "user", + "content": user_query + } + ] + while True: + response = self.client.messages.create( + # system=self.system_prompt, + tools = [self.example_function], + model="claude-3-5-sonnet-20241022", + max_tokens=1024, + messages=messages + ) + print("response content:",response.content[0].text) + + # If there's tool call, Extract the tool call parameters from the response + if len(response.content) > 1 and response.content[1].type == "tool_use": + print("response tools:",response.content[1].input) + tool_call_params = response.content[1].input + tool_call_result = self.get_weather(tool_call_params["location"]) + tool_call_id = response.content[1].id + messages.append({ + "role": response.role, + "content": response.content + } + ) + messages.append({ + "role": "user", + "content": [{ + "type": "tool_result", + "tool_use_id": tool_call_id, + "content": tool_call_result + }] + }) + print("messages:",messages,type(messages)) + else: + return response.content[0].text + + def get_weather(self, location: str): + """Get the current weather in a given location using latitude and longitude.""" + query = f"What is the weather in {location}?" + response = tavily_client.search(query) + # breakpoint() + response_content = response["results"][0]["content"] + return response["results"][0]["title"] + ":\n" + response_content + +# Example usage +def main(): + # Initialize agent with your Anthropic API key + api_key = os.getenv("ANTHROPIC_API_KEY") + weather_agent = WeatherAgent(api_key) + + # Example queries + queries = [ + "What's the weather like in Zurich city?", + "Tell me the forecast for New York", + "How's the weather in London next week?" + ] + + # Process each query + for query in queries: + print(f"\nQuery: {query}") + response = weather_agent.parse_weather_query(query) + print(f"Response: {response}") + +if __name__ == "__main__": + main() \ No newline at end of file