Repository navigation
feat: added Grok as a provider and related functionality #45
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c38f2b9
feat: added Grok as a provider and related functionality
ahmedhamedaly 542bd83
refactor: streamline num_responses field definition in Request model
ahmedhamedaly 43b6fea
refactor: update GrokProvider to inherit from OpenAIProvider
ahmedhamedaly 7a33684
refactor: ci failed due to import format
ahmedhamedaly abecbf8
refactor: update Request models to inherit from StandardRequest
ahmedhamedaly fe383e7
refactor: adjust inheritance order in StandardRequest and update impo…
ahmedhamedaly b212c44
refactor: clean up imports in OpenAI provider module
ahmedhamedaly 4c8b488
small cleanup and added end to end test
johnny-emp 66810be
bump version
johnny-emp 364d63c
format
johnny-emp ee3a60f
fix python version
johnny-emp 392d2f5
add grok API key
johnny-emp 38b4b59
update tox
johnny-emp 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| from .anthropic import AnthropicModelType, AnthropicProvider | ||
| from .deepseek import DeepSeekModelType, DeepSeekProvider | ||
| from .grok import GrokModelType, GrokProvider | ||
| from .openai import OpenAIModelType, OpenAIProvider | ||
| from .openrouter import OpenRouterModelType, OpenRouterProvider | ||
| from .standard_request import StandardRequest | ||
|
|
||
| __all__ = [ | ||
| "AnthropicProvider", | ||
| "DeepSeekProvider", | ||
| "GrokProvider", | ||
| "OpenAIProvider", | ||
| "AnthropicModelType", | ||
| "DeepSeekModelType", | ||
| "GrokModelType", | ||
| "OpenAIModelType", | ||
| "OpenRouterProvider", | ||
| "OpenRouterModelType", | ||
| "StandardRequest", | ||
| ] |
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
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,28 @@ | ||
| import os | ||
| from typing import ClassVar | ||
|
|
||
| from pydantic import Field | ||
|
|
||
| from emp_agents.providers.openai import OpenAIProviderBase | ||
| from emp_agents.providers.openai.response import Response | ||
|
|
||
| from .types import GrokModelType | ||
|
|
||
|
|
||
| class GrokProvider(OpenAIProviderBase[GrokModelType]): | ||
| """ | ||
| Provider for Grok API, which follows the OpenAI API format. | ||
| This inherits from OpenAIProvider since the APIs are compatible. | ||
| """ | ||
|
|
||
| URL: ClassVar[str] = "https://api.x.ai/v1/chat/completions" | ||
|
|
||
| api_key: str = Field(default_factory=lambda: os.environ["GROK_API_KEY"]) | ||
| default_model: GrokModelType = Field(default=GrokModelType.grok_2) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "GrokModelType", | ||
| "GrokProvider", | ||
| "Response", | ||
| ] |
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,16 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic import Field | ||
|
|
||
| from emp_agents.models.shared.tools import GenericTool | ||
| from emp_agents.providers.standard_request import StandardRequest | ||
|
|
||
| from .types import GrokModelType | ||
|
|
||
|
|
||
| class Request(StandardRequest[GrokModelType]): | ||
| """ | ||
| Request model for Grok API, which follows the OpenAI API format. | ||
| """ | ||
|
|
||
| tools: Optional[list[GenericTool]] = Field(default=None) |
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 @@ | ||
| from emp_agents.providers.openai.response import Response as OpenAIResponse | ||
|
|
||
| from .types import GrokModelType | ||
|
|
||
|
|
||
| class Response(OpenAIResponse): | ||
| model: GrokModelType | str |
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,8 @@ | ||
| from enum import StrEnum | ||
|
|
||
|
|
||
| class GrokModelType(StrEnum): | ||
| grok_1 = "grok-1" | ||
| grok_1_5 = "grok-1.5" | ||
| grok_2 = "grok-2" | ||
| grok_3 = "grok-3-latest" |
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
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,34 @@ | ||
| from typing import Annotated, Generic, Literal, Optional, TypeVar | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field, PlainSerializer | ||
|
|
||
| from emp_agents.models.shared import Message | ||
| from emp_agents.models.shared.tools import GenericTool | ||
|
|
||
| ModelType = TypeVar("ModelType") | ||
|
|
||
|
|
||
| class StandardRequest(BaseModel, Generic[ModelType]): | ||
| """ | ||
| Generic request model that can be used by all platforms that mimic the OpenAI format. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
| model: ModelType | ||
| max_tokens: Optional[int] = Field(default=None) | ||
| temperature: Optional[float] = Field(default=None, ge=0, le=2.0) | ||
| tool_choice: Literal["none", "required", "auto", None] = Field(default=None) | ||
|
|
||
| system: str | None = None | ||
| messages: list[Message] | None = None | ||
|
|
||
| frequency_penalty: Optional[float] = Field(default=None, ge=-2.0, le=2.0) | ||
| presence_penalty: Optional[float] = Field(default=None, ge=-2.0, le=2.0) | ||
| num_responses: Optional[int] = Field(default=None, serialization_alias="n") | ||
| top_p: Optional[int] = Field(default=None) | ||
|
|
||
| def model_dump(self, *, exclude_none=True, by_alias=True, **kwargs): | ||
| return super().model_dump( | ||
| exclude_none=exclude_none, by_alias=by_alias, **kwargs | ||
| ) |
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 actually don't need this anymore, since we inverted controls to
Providers