Last active
May 20, 2025 11:55
-
-
Save sa-/c2c25a357b330e0e15656e022393f734 to your computer and use it in GitHub Desktop.
Local agents example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from pydantic_ai import Agent | |
| from pydantic_ai.providers.openai import OpenAIProvider | |
| from pydantic_ai.models.openai import OpenAIModel | |
| from pydantic_ai import Tool | |
| # Run `ollama pull qwen3:14b` to download the model | |
| # Run `ollama serve` if the icon is not already in your menu bar | |
| model = OpenAIModel( | |
| "qwen3:14b", | |
| provider=OpenAIProvider( | |
| api_key="lol", | |
| base_url="http://127.0.0.1:11434/v1", | |
| ), | |
| ) | |
| def get_weather(location: str) -> str: | |
| """Get the weather for a location. | |
| Args: | |
| location (str): In the format for "city, state" or "city, country". | |
| Returns: | |
| str: The weather for the location. | |
| """ | |
| # Note that the doc style & content is important for the tool definition to be generated correctly | |
| # and for the LLM to understand the function. If it's unclear for a person, it's unclear for the LLM. | |
| return f"It's hot in {location}." | |
| if __name__ == "__main__": | |
| # It will pull descriptions from the docstring | |
| weather_tool = Tool(get_weather, docstring_format="google") | |
| agent = Agent( | |
| model, | |
| system_prompt="Be concise, reply with one sentence.", | |
| tools=[weather_tool], | |
| ) | |
| result = agent.run_sync("What's the weather in Berlin?") | |
| for msg in result.all_messages(): | |
| for part in msg.parts: | |
| if part.part_kind == "system-prompt": | |
| print(f"System:\n{part.content}\n") | |
| elif part.part_kind == "user-prompt": | |
| print(f"User:\n{part.content}\n") | |
| elif part.part_kind == "tool-call": | |
| print(f"Tool call: {part.tool_name}, args: {part.args}\n") | |
| elif part.part_kind == "tool-return": | |
| print(f'Tool result: {part.tool_name}, result: "{part.content}"\n') | |
| elif part.part_kind == "text" and part.content != "": | |
| print(f"Response:\n{part.content}\n") | |
| """ | |
| System: | |
| Be concise, reply with one sentence. | |
| User: | |
| What's the weather in Berlin? | |
| Tool call: get_weather, args: {"location":"Berlin, Germany"} | |
| Tool result: get_weather, result: "It's hot in Berlin, Germany." | |
| Response: | |
| <think> | |
| Okay, the user asked for the weather in Berlin. I called the get_weather function with Berlin, Germany as the location. The response came back saying it's hot. Now I need to relay that information clearly. Let me make sure to mention the location again and the temperature. Maybe add a friendly note about the weather. Keep it concise. | |
| </think> | |
| The weather in Berlin, Germany is currently hot. You might want to stay hydrated and wear light clothing! | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment