-
Notifications
You must be signed in to change notification settings - Fork 24
Add Strands & Google ADK Integration #383
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
15 commits
Select commit
Hold shift + click to select a range
0e5b6d2
strands
vrushankportkey d027ef6
Update README.md
vrushankportkey f70a9f4
Update strands.py
vrushankportkey fd4012e
rename to PortkeyStrands
vrushankportkey 23f5e4c
Update README.md
vrushankportkey 3ce8aa6
add google adk
vrushankportkey 1d5e97b
Merge branch 'main' into strands-integrations
vrushankportkey 713746e
fix the issue
vrushankportkey 8b3f727
Merge branch 'strands-integrations' of https://github.com/Portkey-AI/…
vrushankportkey 7613477
add tests and sample code
vrushankportkey c98d2fc
fix: formatting and linting
csgulati09 06b7367
fix: 3.8 linting fix
csgulati09 27a228d
strands example
vrushankportkey 8ecf253
Merge branch 'strands-integrations' of https://github.com/Portkey-AI/…
vrushankportkey 5572643
fix: linting and formatting
csgulati09 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import asyncio | ||
| import os | ||
| import sys | ||
| from typing import List | ||
|
|
||
| try: | ||
| from google.adk.models.llm_request import LlmRequest | ||
| from google.genai import types | ||
| from portkey_ai.integrations.adk import PortkeyAdk | ||
| except Exception: # pragma: no cover - example script | ||
| print("This example requires the 'adk' extra: pip install 'portkey-ai[adk]'") | ||
| raise | ||
|
|
||
|
|
||
| def build_request(model: str) -> "LlmRequest": # type: ignore[name-defined] | ||
| return LlmRequest( | ||
| model=model, | ||
| contents=[ | ||
| types.Content( | ||
| role="user", | ||
| parts=[ | ||
| types.Part.from_text( | ||
| text="Give me a one-line programming joke (final only)." | ||
| ) | ||
| ], | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| api_key = os.environ.get("PORTKEY_API_KEY") | ||
| model = os.environ.get("MODEL_SLUG", "@openai/gpt-4o-mini") | ||
|
|
||
| if not api_key: | ||
| print("Set PORTKEY_API_KEY in your environment.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| llm = PortkeyAdk(api_key=api_key, model=model) | ||
|
|
||
| # Non-streaming: returns a single final response | ||
| req = build_request(model) | ||
| final_text: List[str] = [] | ||
| async for resp in llm.generate_content_async(req, stream=False): | ||
| if resp.content and getattr(resp.content, "parts", None): | ||
| for p in resp.content.parts: | ||
| if getattr(p, "text", None): | ||
| final_text.append(p.text) | ||
| print("".join(final_text)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": # pragma: no cover - example script | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import asyncio | ||
| import os | ||
| import sys | ||
|
|
||
| try: | ||
| from portkey_ai.integrations.strands import PortkeyStrands | ||
| except Exception: | ||
| print( | ||
| "This example requires the 'strands' extra: pip install 'portkey-ai[strands]'", | ||
| file=sys.stderr, | ||
| ) | ||
| raise | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| api_key = os.environ.get("PORTKEY_API_KEY") | ||
| model_id = os.environ.get("MODEL_SLUG", "@openai/gpt-4o-mini") | ||
|
|
||
| if not api_key: | ||
| print("Set PORTKEY_API_KEY in your environment.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| model = PortkeyStrands( | ||
| api_key=api_key, # type: ignore[arg-type] | ||
| model_id=model_id, # type: ignore[arg-type] | ||
| ) | ||
|
|
||
| # Minimal Strands-compatible message list (no Agent required) | ||
| messages = [ | ||
| {"role": "user", "content": "Tell me a short programming joke."}, | ||
| ] | ||
|
|
||
| print(f"Streaming with model: {model_id}") | ||
| async for event in model.stream(messages=messages): | ||
| # Events follow the Strands stream event shape produced by our adapter. | ||
| if isinstance(event, dict) and "contentBlockDelta" in event: | ||
| delta = event["contentBlockDelta"].get("delta", {}) | ||
| if isinstance(delta, dict) and "text" in delta: | ||
| print(delta["text"], end="") | ||
| print("\n-- stream done --") | ||
|
|
||
|
|
||
| if __name__ == "__main__": # pragma: no cover - example script | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| """Integrations namespace. | ||
|
|
||
| Optional adapters for third-party frameworks live here. Install extras to use. | ||
| """ |
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.
Uh oh!
There was an error while loading. Please reload this page.