Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import wolframalpha
- def weather_lookup(location):
- api_key = "your_weather_api_key"
- url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={location}"
- response = requests.get(url)
- data = response.json()
- return f"The current temperature in {location} is {data['current']['temp_c']}°C"
- def wolfram_query(query):
- client = wolframalpha.Client("your_wolfram_alpha_app_id")
- res = client.query(query)
- return next(res.results).text
- tools = {
- "weather_lookup": weather_lookup,
- "wolfram_query": wolfram_query
- }
- def tool_using_agent(user_input):
- tool_selection_prompt = f"""
- Based on the user's input, determine if a tool should be used to answer the question.
- Available tools: {', '.join(tools.keys())}
- User input: {user_input}
- If a tool should be used, respond with the tool name and the parameter to pass to the tool.
- If no tool is needed, respond with "No tool needed."
- Response:
- """
- tool_decision = llm.generate(tool_selection_prompt)
- if tool_decision.startswith("No tool needed"):
- return llm.generate(f"User: {user_input}\nAssistant:")
- else:
- tool_name, parameter = tool_decision.split(', ')
- tool_result = tools[tool_name](parameter)
- final_response_prompt = f"""
- User asked: {user_input}
- Tool used: {tool_name}
- Tool result: {tool_result}
- Please provide a natural language response to the user based on this information:
- """
- return llm.generate(final_response_prompt)
- # Usage
- print(tool_using_agent("What's the weather like in New York?"))
- print(tool_using_agent("What's the square root of 256?"))
Advertisement
Add Comment
Please, Sign In to add comment