-
Notifications
You must be signed in to change notification settings - Fork 409
Add AIML API provider-specific example scripts #1243
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
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,47 @@ | ||
| """ | ||
| » AIML_API_KEY=your-api-key \ | ||
| uv run examples/provider_specific/aimlapi/run_agent.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from pydantic_ai.models.openai import OpenAIModel | ||
| from pydantic_ai.providers.openai import OpenAIProvider | ||
|
|
||
| import marvin | ||
|
|
||
| AIML_API_URL = "https://api.aimlapi.com/v1" | ||
|
|
||
|
|
||
| def get_provider() -> OpenAIProvider: | ||
| api_key = os.getenv("AIML_API_KEY") | ||
| if not api_key: | ||
| raise RuntimeError("Set AIML_API_KEY environment variable to your AI/ML API key.") | ||
| return OpenAIProvider(api_key=api_key, base_url=AIML_API_URL) | ||
|
|
||
|
|
||
| def write_file(path: str, content: str) -> None: | ||
| """Write content to a file.""" | ||
| Path(path).write_text(content) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| writer = marvin.Agent( | ||
| model=OpenAIModel("gpt-4o", provider=get_provider()), | ||
| name="AI/ML Writer", | ||
| instructions="Write concise, engaging content for developers", | ||
| tools=[write_file], | ||
| ) | ||
|
|
||
| result = marvin.run( | ||
| "how to use pydantic? write haiku to docs.md", | ||
| agents=[writer], | ||
| ) | ||
| print(result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| » AIML_API_KEY=your-api-key \ | ||
| uv run examples/provider_specific/aimlapi/structured_output.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing_extensions import TypedDict | ||
|
|
||
| from pydantic_ai.models.openai import OpenAIModel | ||
| from pydantic_ai.providers.openai import OpenAIProvider | ||
|
|
||
| import marvin | ||
|
|
||
| AIML_API_URL = "https://api.aimlapi.com/v1" | ||
|
|
||
|
|
||
| class LearningResource(TypedDict): | ||
| title: str | ||
| url: str | ||
| summary: str | ||
|
|
||
|
|
||
| def get_provider() -> OpenAIProvider: | ||
| api_key = os.getenv("AIML_API_KEY") | ||
| if not api_key: | ||
| raise RuntimeError("Set AIML_API_KEY environment variable to your AI/ML API key.") | ||
| return OpenAIProvider(api_key=api_key, base_url=AIML_API_URL) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| researcher = marvin.Agent( | ||
| model=OpenAIModel("gpt-4o", provider=get_provider()), | ||
| name="Resource Researcher", | ||
| instructions=( | ||
| "Return structured JSON describing useful developer resources for the AI/ML API." | ||
| ), | ||
| ) | ||
|
|
||
| resources = marvin.run( | ||
| "share three quickstart resources for building with the AI/ML API", | ||
| result_type=list[LearningResource], | ||
| agents=[researcher], | ||
| ) | ||
|
|
||
| for resource in resources: | ||
| print(f"- {resource['title']}\n {resource['url']}\n {resource['summary']}\n") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """ | ||
| » AIML_API_KEY=your-api-key \ | ||
| uv run examples/provider_specific/aimlapi/tools_agent.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from datetime import date, timedelta | ||
|
|
||
| from pydantic_ai.models.openai import OpenAIModel | ||
| from pydantic_ai.providers.openai import OpenAIProvider | ||
|
|
||
| import marvin | ||
|
|
||
| AIML_API_URL = "https://api.aimlapi.com/v1" | ||
|
|
||
|
|
||
| def get_provider() -> OpenAIProvider: | ||
| api_key = os.getenv("AIML_API_KEY") | ||
| if not api_key: | ||
| raise RuntimeError("Set AIML_API_KEY environment variable to your AI/ML API key.") | ||
| return OpenAIProvider(api_key=api_key, base_url=AIML_API_URL) | ||
|
|
||
|
|
||
| def get_event_date(offset_days: int = 0) -> str: | ||
| """Return an ISO formatted date offset from today.""" | ||
| today = date.today() | ||
| return (today + timedelta(days=offset_days)).isoformat() | ||
|
|
||
|
|
||
| def mock_weather_lookup(city: str) -> str: | ||
| """Pretend to look up weather for a city.""" | ||
| return f"The forecast in {city} calls for mild temperatures and light winds." | ||
|
|
||
|
|
||
| def main() -> None: | ||
| planner = marvin.Agent( | ||
| model=OpenAIModel("gpt-4o", provider=get_provider()), | ||
| name="AI/ML Event Planner", | ||
| instructions=( | ||
| "Plan concise community events for AI/ML API enthusiasts." | ||
| " Use the available tools for dates and weather when helpful." | ||
| ), | ||
| tools=[get_event_date, mock_weather_lookup], | ||
| ) | ||
|
|
||
| plan = marvin.run( | ||
| "Design a Saturday workshop introducing the AI/ML API in Berlin", | ||
| agents=[planner], | ||
| ) | ||
| print(plan) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
[nitpick] The nested f-string with multiple escape sequences reduces readability. Consider using a multi-line formatted string or separate print statements for cleaner code, e.g.,
print(f\"- {resource['title']}\"),print(f\" {resource['url']}\"),print(f\" {resource['summary']}\n\").