-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Introduce ManagedAgent and AgentRunner implementations #110
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 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
494780e
feat: Introduce ManagedAgent and AgentRunner implementations
jsonbailey 0f7752d
feat: update managed-agent to use track_metrics_of_async and add prov…
jsonbailey 9523fa7
fix: resolve lint errors from rebase onto main
jsonbailey b1712dc
simplifying tool configuration
jsonbailey afe344e
simplify agent loop to use built-ins
jsonbailey d9ab2a8
address feedback
jsonbailey aca83ea
address PR feedback
jsonbailey 268b669
remove unused client
jsonbailey 9295cc3
fix token tracking
jsonbailey 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
63 changes: 63 additions & 0 deletions
63
packages/ai-providers/server-ai-langchain/src/ldai_langchain/langchain_agent_runner.py
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,63 @@ | ||
| """LangChain agent runner for LaunchDarkly AI SDK.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from ldai import log | ||
| from ldai.providers import AgentResult, AgentRunner | ||
| from ldai.providers.types import LDAIMetrics | ||
|
|
||
| from ldai_langchain.langchain_helper import sum_token_usage_from_messages | ||
|
|
||
|
|
||
| class LangChainAgentRunner(AgentRunner): | ||
| """ | ||
| AgentRunner implementation for LangChain. | ||
|
|
||
| Wraps a compiled LangChain agent graph (from ``langchain.agents.create_agent``) | ||
| and delegates execution to it. Tool calling and loop management are handled | ||
| internally by the graph. | ||
| Returned by LangChainRunnerFactory.create_agent(config, tools). | ||
| """ | ||
|
|
||
| def __init__(self, agent: Any): | ||
| self._agent = agent | ||
|
|
||
| async def run(self, input: Any) -> AgentResult: | ||
| """ | ||
| Run the agent with the given input string. | ||
|
|
||
| Delegates to the compiled LangChain agent, which handles | ||
| the tool-calling loop internally. | ||
|
|
||
| :param input: The user prompt or input to the agent | ||
| :return: AgentResult with output, raw response, and aggregated metrics | ||
| """ | ||
| try: | ||
| result = await self._agent.ainvoke({ | ||
| "messages": [{"role": "user", "content": str(input)}] | ||
| }) | ||
| messages = result.get("messages", []) | ||
| output = "" | ||
| if messages: | ||
| last = messages[-1] | ||
| if hasattr(last, 'content') and isinstance(last.content, str): | ||
| output = last.content | ||
| return AgentResult( | ||
| output=output, | ||
| raw=result, | ||
| metrics=LDAIMetrics( | ||
| success=True, | ||
| usage=sum_token_usage_from_messages(messages), | ||
| ), | ||
| ) | ||
| except Exception as error: | ||
| log.warning(f"LangChain agent run failed: {error}") | ||
| return AgentResult( | ||
| output="", | ||
| raw=None, | ||
| metrics=LDAIMetrics(success=False, usage=None), | ||
| ) | ||
|
|
||
| def get_agent(self) -> Any: | ||
| """Return the underlying compiled LangChain agent.""" | ||
| return self._agent |
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.
Uh oh!
There was an error while loading. Please reload this page.