-
Notifications
You must be signed in to change notification settings - Fork 100
Add GithubAgent step #1366
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
Add GithubAgent step #1366
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,46 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import subprocess | ||
|
|
||
| from patchwork.common.tools.tool import Tool | ||
|
|
||
|
|
||
| class GitHubTool(Tool, tool_name="github_tool"): | ||
| def __init__(self, path: str, gh_token: str): | ||
| super().__init__() | ||
| self.path = path | ||
| self.gh_token = gh_token | ||
|
|
||
| @property | ||
| def json_schema(self) -> dict: | ||
| return { | ||
| "name": "github_tool", | ||
| "description": """\ | ||
| Access to the GitHub CLI, the command is also `gh` all args provided are used as is | ||
| """, | ||
| "input_schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "args": { | ||
| "type": "array", | ||
| "items": {"type": "string"}, | ||
| "description": "The args to run `gh` command with.", | ||
| } | ||
| }, | ||
| "required": ["args"], | ||
| }, | ||
| } | ||
|
|
||
| def execute(self, args: list[str]) -> str: | ||
| env = os.environ.copy() | ||
| env["GH_TOKEN"] = self.gh_token | ||
| p = subprocess.run( | ||
| ["gh", *args], | ||
| env=env, | ||
| cwd=self.path, | ||
| text=True, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| ) | ||
| return p.stdout | ||
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
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,47 @@ | ||
| from pathlib import Path | ||
|
|
||
| from patchwork.common.client.llm.aio import AioLlmClient | ||
| from patchwork.common.multiturn_strategy.agentic_strategy_v2 import ( | ||
| AgentConfig, | ||
| AgenticStrategyV2, | ||
| ) | ||
| from patchwork.common.tools.github_tool import GitHubTool | ||
| from patchwork.step import Step | ||
| from patchwork.steps.GitHubAgent.typed import GitHubAgentInputs, GitHubAgentOutputs | ||
|
|
||
|
|
||
| class GitHubAgent(Step, input_class=GitHubAgentInputs, output_class=GitHubAgentOutputs): | ||
| def __init__(self, inputs): | ||
| super().__init__(inputs) | ||
| base_path = inputs.get("base_path", str(Path.cwd())) | ||
| task = inputs["task"] | ||
| self.agentic_strategy = AgenticStrategyV2( | ||
| model="claude-3-7-sonnet-latest", | ||
| llm_client=AioLlmClient.create_aio_client(inputs), | ||
| template_data=dict(), | ||
| system_prompt_template=f"""\ | ||
| Please summarise the conversation given and provide the result in the structure that is asked of you. | ||
| """, | ||
| user_prompt_template=f"""\ | ||
| Please help me with the following task using the GitHub CLI. You should not do anything extra. | ||
| Please take note of any requirements to the data required to fetch. | ||
|
|
||
| {task} | ||
| """, | ||
| agent_configs=[ | ||
| AgentConfig( | ||
| name="Assistant", | ||
| tool_set=dict(github_tool=GitHubTool(base_path, inputs["github_api_token"])), | ||
| system_prompt="""\ | ||
| You are a senior software developer helping the program manager to obtain some data from GitHub. | ||
| You can access github through the `gh` CLI app. | ||
| Your `gh` app has already been authenticated. | ||
| """, | ||
| ) | ||
| ], | ||
| example_json=inputs.get("example_json"), | ||
| ) | ||
|
|
||
| def run(self) -> dict: | ||
| result = self.agentic_strategy.execute(limit=10) | ||
| return {**result, **self.agentic_strategy.usage()} |
Empty file.
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,27 @@ | ||
| from typing_extensions import Annotated, Any, Dict, List, TypedDict | ||
|
|
||
| from patchwork.common.utils.step_typing import StepTypeConfig | ||
|
|
||
|
|
||
| class GitHubAgentInputs(TypedDict, total=False): | ||
| base_path: str | ||
| prompt_value: Dict[str, Any] | ||
| system_prompt: str | ||
| user_prompt: str | ||
| max_llm_calls: Annotated[int, StepTypeConfig(is_config=True)] | ||
| openai_api_key: Annotated[ | ||
| str, StepTypeConfig(is_config=True, or_op=["patched_api_key", "google_api_key", "anthropic_api_key"]) | ||
| ] | ||
| anthropic_api_key: Annotated[ | ||
| str, StepTypeConfig(is_config=True, or_op=["patched_api_key", "google_api_key", "openai_api_key"]) | ||
| ] | ||
| google_api_key: Annotated[ | ||
| str, StepTypeConfig(is_config=True, or_op=["patched_api_key", "openai_api_key", "anthropic_api_key"]) | ||
| ] | ||
|
|
||
|
|
||
| class GitHubAgentOutputs(TypedDict): | ||
| conversation_history: List[Dict] | ||
| tool_records: List[Dict] | ||
| request_tokens: int | ||
| response_tokens: int |
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.
abc_register=False?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.
its alright since
Tool.get_toolswill exclude it whengh_tokenis not provided