-
Notifications
You must be signed in to change notification settings - Fork 1.4k
XaiModel with tests and stock analysis agent #3400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brightsparc
wants to merge
20
commits into
pydantic:main
Choose a base branch
from
brightsparc:xai-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,783
β81
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
311395d
Initial commit for xAI grok model
brightsparc afd8a4e
Adding flight booking example for live test
brightsparc ab2ff90
Merge remote-tracking branch 'upstream/main' into xai-sdk
brightsparc 4513c8f
Fix pre-commit issues
brightsparc e60d3ea
Updated grok to support passing in AsyncClient, added initial tests
brightsparc 43e6890
Fix pyright
brightsparc 4f15966
Update grok all available grok models
brightsparc 185a6be
Adding image tests that require an active API_KEY
brightsparc a632cbf
Adding MCP added stock analysis agent
brightsparc eb10f47
Merge remote-tracking branch 'upstream/main' into xai-sdk
brightsparc 18b21a5
Updating docs
brightsparc b417485
Set provider response id
brightsparc 0f1c113
Adding reasoning and usage
brightsparc 2b58f59
Update stock analytics agent to stream responses
brightsparc 735da97
Merge remote-tracking branch 'upstream/main' into xai-sdk
brightsparc 65bb646
Remove new line for tool calls
brightsparc b19479e
Adding XAI_API_KEY to test_examples.py
brightsparc 8268267
Adding grok-4-1-fast-non-reasoning as default
brightsparc 843f588
Update to mention GrokModelName in docs
brightsparc 99ad3aa
Renmae GrokModel to XaiModel, and update dependencies and tests. Addβ¦
brightsparc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # `pydantic_ai.models.grok` | ||
|
|
||
| ## Setup | ||
|
|
||
| For details on how to set up authentication with this model, see [model configuration for Grok](../../models/grok.md). | ||
|
|
||
| ::: pydantic_ai.models.grok |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # xAI | ||
|
|
||
| ## Install | ||
|
|
||
| To use [`XaiModel`][pydantic_ai.models.xai.XaiModel], you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `xai` optional group: | ||
|
|
||
| ```bash | ||
| pip/uv-add "pydantic-ai-slim[xai]" | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| To use xAI models from [xAI](https://x.ai/api) through their API, go to [console.x.ai](https://console.x.ai/team/default/api-keys) to create an API key. | ||
|
|
||
| [`GrokModelName`][pydantic_ai.providers.grok.GrokModelName] contains a list of available xAI models. | ||
|
|
||
| ## Environment variable | ||
|
|
||
| Once you have the API key, you can set it as an environment variable: | ||
|
|
||
| ```bash | ||
| export XAI_API_KEY='your-api-key' | ||
| ``` | ||
|
|
||
| You can then use [`XaiModel`][pydantic_ai.models.xai.XaiModel] by name: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
|
|
||
| agent = Agent('xai:grok-4-1-fast-non-reasoning') | ||
| ... | ||
| ``` | ||
|
|
||
| Or initialise the model directly: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.xai import XaiModel | ||
|
|
||
| # Uses XAI_API_KEY environment variable | ||
| model = XaiModel('grok-4-1-fast-non-reasoning') | ||
| agent = Agent(model) | ||
| ... | ||
| ``` | ||
|
|
||
| You can also customize the [`XaiModel`][pydantic_ai.models.xai.XaiModel] with a custom provider: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.xai import XaiModel | ||
| from pydantic_ai.providers.xai import XaiProvider | ||
|
|
||
| # Custom API key | ||
| provider = XaiProvider(api_key='your-api-key') | ||
| model = XaiModel('grok-4-1-fast-non-reasoning', provider=provider) | ||
| agent = Agent(model) | ||
| ... | ||
| ``` | ||
|
|
||
| Or with a custom `xai_sdk.AsyncClient`: | ||
|
|
||
| ```python | ||
| from xai_sdk import AsyncClient | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.xai import XaiModel | ||
| from pydantic_ai.providers.xai import XaiProvider | ||
|
|
||
| xai_client = AsyncClient(api_key='your-api-key') | ||
| provider = XaiProvider(xai_client=xai_client) | ||
| model = XaiModel('grok-4-1-fast-non-reasoning', provider=provider) | ||
| agent = Agent(model) | ||
| ... | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """Example of using Grok's server-side web_search tool. | ||
|
|
||
| This agent: | ||
| 1. Uses web_search to find the hottest performing stock yesterday | ||
| 2. Provides buy analysis for the user | ||
| """ | ||
|
|
||
| import logfire | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from pydantic_ai import ( | ||
| Agent, | ||
| BuiltinToolCallPart, | ||
| WebSearchTool, | ||
| ) | ||
| from pydantic_ai.models.xai import XaiModel | ||
|
|
||
| logfire.configure() | ||
| logfire.instrument_pydantic_ai() | ||
|
|
||
| # Configure for xAI API - XAI_API_KEY environment variable is required | ||
| # The model will automatically use XaiProvider with the API key from the environment | ||
|
|
||
| # Create the model using XaiModel with server-side tools | ||
| model = XaiModel('grok-4-fast') | ||
|
|
||
|
|
||
| class StockAnalysis(BaseModel): | ||
| """Analysis of top performing stock.""" | ||
|
|
||
| stock_symbol: str = Field(description='Stock ticker symbol') | ||
| current_price: float = Field(description='Current stock price') | ||
| buy_analysis: str = Field(description='Brief analysis for whether to buy the stock') | ||
|
|
||
|
|
||
| # This agent uses server-side web search to research stocks | ||
| stock_analysis_agent = Agent[None, StockAnalysis]( | ||
| model=model, | ||
| output_type=StockAnalysis, | ||
| builtin_tools=[WebSearchTool()], | ||
| system_prompt=( | ||
| 'You are a stock analysis assistant. ' | ||
| 'Use web_search to find the hottest performing stock from yesterday on NASDAQ. ' | ||
| 'Provide the current price and a brief buy analysis explaining whether this is a good buy.' | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def main(): | ||
| """Run the stock analysis agent.""" | ||
| query = 'What was the hottest performing stock on NASDAQ yesterday?' | ||
|
|
||
| print('π Starting stock analysis...\n') | ||
| print(f'Query: {query}\n') | ||
|
|
||
| async with stock_analysis_agent.run_stream(query) as result: | ||
| # Stream responses as they happen | ||
| async for message, _is_last in result.stream_responses(): | ||
| for part in message.parts: | ||
| if isinstance(part, BuiltinToolCallPart): | ||
| print(f'π§ Server-side tool: {part.tool_name}') | ||
|
|
||
| # Access output after streaming is complete | ||
| output = await result.get_output() | ||
|
|
||
| print('\nβ Analysis complete!\n') | ||
|
|
||
| print(f'π Top Stock: {output.stock_symbol}') | ||
| print(f'π° Current Price: ${output.current_price:.2f}') | ||
| print(f'\nπ Buy Analysis:\n{output.buy_analysis}') | ||
|
|
||
| # Show usage statistics | ||
| usage = result.usage() | ||
| print('\nπ Usage Statistics:') | ||
| print(f' Requests: {usage.requests}') | ||
| print(f' Input Tokens: {usage.input_tokens}') | ||
| print(f' Output Tokens: {usage.output_tokens}') | ||
| print(f' Total Tokens: {usage.total_tokens}') | ||
|
|
||
| # Show server-side tools usage if available | ||
| if usage.details and 'server_side_tools_used' in usage.details: | ||
| print(f' Server-Side Tools: {usage.details["server_side_tools_used"]}') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| import asyncio | ||
|
|
||
| asyncio.run(main()) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -175,6 +175,24 @@ | |
| 'grok:grok-3-mini-fast', | ||
| 'grok:grok-4', | ||
| 'grok:grok-4-0709', | ||
| 'grok:grok-4-1-fast-non-reasoning', | ||
| 'grok:grok-4-1-fast-reasoning', | ||
| 'grok:grok-4-fast-non-reasoning', | ||
| 'grok:grok-4-fast-reasoning', | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 'grok:grok-code-fast-1', | ||
| 'xai:grok-2-image-1212', | ||
| 'xai:grok-2-vision-1212', | ||
| 'xai:grok-3', | ||
| 'xai:grok-3-fast', | ||
| 'xai:grok-3-mini', | ||
| 'xai:grok-3-mini-fast', | ||
| 'xai:grok-4', | ||
| 'xai:grok-4-0709', | ||
| 'xai:grok-4-1-fast-non-reasoning', | ||
| 'xai:grok-4-1-fast-reasoning', | ||
| 'xai:grok-4-fast-non-reasoning', | ||
| 'xai:grok-4-fast-reasoning', | ||
| 'xai:grok-code-fast-1', | ||
| 'groq:deepseek-r1-distill-llama-70b', | ||
| 'groq:deepseek-r1-distill-qwen-32b', | ||
| 'groq:distil-whisper-large-v3-en', | ||
|
|
@@ -804,6 +822,7 @@ def infer_model( # noqa: C901 | |
| 'fireworks', | ||
| 'github', | ||
| 'grok', | ||
| 'xai', | ||
| 'heroku', | ||
| 'moonshotai', | ||
| 'ollama', | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to include this example, we should have a doc under
docs/examplesas well. I'm also OK with not including this example.