-
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
48
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.
Open
Changes from 17 commits
Commits
Show all changes
48 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 d933b2c
Remove use of hasattr
brightsparc fe93dad
Fix RetryPromptPart
brightsparc 5224e56
Add support for DocumentUrl, and downloading an dummy pdf with casette
brightsparc 128c05a
Implement first pass of response parts using openai chat completions β¦
brightsparc b35bcfd
Adding support for formatting builtin tools
brightsparc 59bf2b6
Update docs for xAI refence
brightsparc e4ebcca
Minor tweak to tidy process response
brightsparc f69a730
Adding prepare_request
brightsparc 052086b
Updating live tests to use mocks, with a design document on the ratioβ¦
brightsparc 4521909
Reduce duplication between request and request_stream, by creating a β¦
brightsparc c9326e1
Adding support for system instructions
brightsparc a53badc
Using system for provider_name, passing in response.timestamp insteadβ¦
brightsparc 26744cc
Add built in tool name and finish reason map
brightsparc ca70b96
Fix tests
brightsparc 999eb81
Adding tool_choice and response_format arguments to create_chat
brightsparc c335ca6
Updating docs for xai.md
brightsparc bcc7834
Update case of xAI
brightsparc 9d06c87
Adding tests for tool_choice and native json output
brightsparc df20aab
Updating docs link for models
brightsparc 656eb62
Create XaiModelName and deprecated GrokProvider
brightsparc 34035c0
Update builtin tests to verify output parts with snapshot
brightsparc 5452ec1
Fix mcp_server tool with prefix and snapshot
brightsparc bf6d4fd
Adding streaming tests for web_search and code_execution
brightsparc da5a14f
Update remaining test to use snapshot
brightsparc 99c83f6
Adding tests for run_stream_events and added constants for model names
brightsparc fb027c2
Merge from main
brightsparc 9ceb5df
Updating documentation for stock_analysis
brightsparc 88f49af
Updating to unlress xai-sdk version
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Grok | ||
brightsparc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Install | ||
|
|
||
| To use `GrokModel`, you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `grok` optional group: | ||
|
|
||
| ```bash | ||
| pip/uv-add "pydantic-ai-slim[grok]" | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| To use Grok from [xAI](https://x.ai/api) through their API, go to [console.x.ai]https://console.x.ai) and follow your nose until you find the place to create an API key. | ||
|
|
||
| ## 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 `GrokModel` by name: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
|
|
||
| agent = Agent('grok:grok-4-fast-non-reasoning') | ||
| ... | ||
| ``` | ||
|
|
||
| Or initialise the model directly with just the model name: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.grok import GrokModel | ||
|
|
||
| model = GrokModel('grok-4-fast-non-reasoning') | ||
| agent = Agent(model) | ||
| ... | ||
| ``` | ||
|
|
||
| You can provide your own `api_key` inline like so: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.grok import GrokModel | ||
|
|
||
| model = GrokModel('grok-4-fast-non-reasoning', api_key='your-api-key') | ||
| agent = Agent(model) | ||
| ... | ||
| ``` | ||
|
|
||
| You can also customize the `GrokModel` with a custom `xai_sdk.AsyncClient`: | ||
|
|
||
| ```python | ||
| from xai_sdk import AsyncClient | ||
| async_client = AsyncClient(api_key='your-api-key') | ||
|
|
||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.grok import GrokModel | ||
|
|
||
| model = GrokModel('grok-4-fast-non-reasoning', client=async_client) | ||
| 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
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,93 @@ | ||
| """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 os | ||
|
|
||
| import logfire | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from pydantic_ai import ( | ||
| Agent, | ||
| BuiltinToolCallPart, | ||
| WebSearchTool, | ||
| ) | ||
| from pydantic_ai.models.grok import GrokModel | ||
|
|
||
| logfire.configure() | ||
| logfire.instrument_pydantic_ai() | ||
|
|
||
| # Configure for xAI API | ||
| xai_api_key = os.getenv('XAI_API_KEY') | ||
| if not xai_api_key: | ||
| raise ValueError('XAI_API_KEY environment variable is required') | ||
|
|
||
|
|
||
| # Create the model using GrokModel with server-side tools | ||
| model = GrokModel('grok-4-fast', api_key=xai_api_key) | ||
|
|
||
|
|
||
| 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
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.
We should add both new docs pages to
mkdocs.ymlso they show up in the navbar