-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28b8b87
commit ea8bb39
Showing
5 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
# Python-related | ||
__pycache__/ | ||
*.py[cod] | ||
*.so | ||
*.egg | ||
*.egg-info/ | ||
dist/ | ||
build/ | ||
*.egg-info/ | ||
.eggs/ | ||
|
||
# Virtual environments | ||
venv/ | ||
env/ | ||
.venv/ | ||
.env/ | ||
|
||
# IDEs and editors | ||
.vscode/ | ||
.idea/ | ||
*.swp | ||
*.swo | ||
*~ | ||
|
||
# OS-specific | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Project-specific | ||
logs/*.log | ||
!logs/.gitkeep | ||
|
||
# API keys and secrets | ||
.env | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# Log files | ||
*.log | ||
.aider* |
Empty file.
This file contains 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,80 @@ | ||
import re | ||
import json | ||
from openai import OpenAI | ||
|
||
class Agent: | ||
def __init__(self, name, agent_prompt): | ||
self.name = name | ||
self.agent_prompt = agent_prompt | ||
self.client = OpenAI() | ||
self.messages = [] | ||
self.steps = [] | ||
|
||
def get_solution(self, problem, previous_solution=None): | ||
self.initialize_conversation(problem, previous_solution) | ||
prompt = "\n\n".join([f"{msg['role']}: {msg['content']}" for msg in self.messages]) | ||
response = self.generate_response(prompt) | ||
|
||
if not response: | ||
print(f"{self.name} did not return a valid response.") | ||
|
||
step_data = self.parse_response(response) | ||
|
||
if not step_data: | ||
print(f"{self.name} failed to parse response: {response}") | ||
|
||
self.store_step(step_data) | ||
|
||
return self.compile_solution() | ||
|
||
def initialize_conversation(self, problem, previous_solution): | ||
self.messages = [] | ||
# Initialize system prompt | ||
self.messages.append({"role": "system", "content": self.agent_prompt}) | ||
|
||
# Add problem and previous solution (if any) | ||
user_message = f"问题:\n{problem}\n" | ||
if previous_solution: | ||
user_message += f"\n之前的解决方案:\n{previous_solution}\n请继续进行你的推理分析。" | ||
else: | ||
user_message += "\n请提供你的解决方案。" | ||
|
||
self.messages.append({"role": "user", "content": user_message}) | ||
# Assistant initial acknowledgment | ||
self.messages.append({"role": "assistant", "content": "好的,我现在开始推理"}) | ||
|
||
def generate_response(self, prompt): | ||
# Generate response from the model | ||
try: | ||
response = self.client.chat.completions.create( | ||
model="gpt-4o", | ||
messages=[{"role": "user", "content": prompt}] | ||
) | ||
return response.choices[0].message.content | ||
except Exception as e: | ||
print(f"Error generating response: {str(e)}") | ||
return None | ||
|
||
def parse_response(self, result): | ||
# Attempt to parse the assistant's response as JSON | ||
try: | ||
json_pattern = r"```json\s*(\{[\s\S]*?\})\s*```" | ||
match = re.findall(json_pattern, result) | ||
if match: | ||
json_list = [] | ||
for temp_match in match: | ||
json_list.append(json.loads(temp_match)) | ||
return json_list | ||
except json.JSONDecodeError as e: | ||
print(f"{self.name} error parsing JSON response: {str(e)} - Response Text: {result}") | ||
return None | ||
|
||
def store_step(self, step_data): | ||
# Store the parsed step data and append it to the conversation | ||
for data in step_data: | ||
self.steps.append((data['title'], data["content"])) | ||
self.messages.append({"role": "assistant", "content": data["content"]}) | ||
|
||
def compile_solution(self): | ||
# Compile the steps into a final solution format | ||
return "\n\n".join([f"### {title}\n{content}" for title, content in self.steps]) |
This file contains 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,138 @@ | ||
from .agent import Agent | ||
|
||
|
||
class AgentBuilder: | ||
def __init__(self): | ||
self.agent_prompts = [ | ||
( | ||
"初步方案设计师", | ||
""" | ||
你是初步方案设计师,一名擅长通过详细的链式思维推理提供初步解决方案的专家。 | ||
**目标** | ||
- 深入理解问题。 | ||
- 提供详细的、逐步的解决方案。 | ||
- 反思并迭代你的解决方案,直到有信心确认无误为止。 | ||
**指示** | ||
- 在每个推理步骤之后,决定是否需要继续完善推理、是否准备将解决方案传递给下一个代理人。 | ||
- 明确说明推理中的任何不确定性或假设。 | ||
**输出格式** | ||
以JSON格式回复: | ||
- "title":该推理步骤的简短标题。 | ||
- "content":该推理步骤的详细解释。 | ||
- "next_action":如果你需要继续执行更多步骤,填"continue";如果你对解决方案有信心,则填"final_answer"。 | ||
**响应示例** | ||
```json | ||
{ | ||
"title": "问题分析", | ||
"content": "为了解决这个问题,我将首先……", | ||
"next_action": "continue" | ||
} | ||
""", | ||
), | ||
( | ||
"批判性反思分析师", | ||
""" | ||
你是一位批判性反思分析师,负责审查解决方案并通过深度反思来改进它们。 | ||
**目标** | ||
- 批判性地分析前一个解决方案。 | ||
- 识别优点和需要改进的地方。 | ||
- 提供具体的改进建议。 | ||
- 对你的解决方案进行反思和迭代,直到你充满信心。 | ||
- 考虑与其他agent协商的可能性,特别是在遇到重大分歧时。 | ||
**指示** | ||
1. **解决方案审查** | ||
- 总结前一个解决方案的要点。 | ||
- 识别所有错误、遗漏或需要改进的地方。 | ||
2. **改进** | ||
- 为解决方案提供具体的改进建议。 | ||
- 纠正存在的错误或误解。 | ||
3. **反思与迭代** | ||
- 批判性地评估你修改后的解决方案。 | ||
- 决定是继续完善还是进入下一个阶段。 | ||
4. **最终答案** | ||
- 清晰地表述你改进后的解决方案。 | ||
5. **协商需求** | ||
- 如果存在重大问题,可以考虑是否需要与初步方案设计师协商。 | ||
**输出格式** | ||
使用JSON格式回应: | ||
- "标题": 推理步骤的简短标题。 | ||
- "内容": 推理步骤的详细解释。 | ||
- "下一步行动": 如果你需要继续执行更多步骤,填"continue";如果你对解决方案有信心,则填"final_answer"。 | ||
**输出示例** | ||
```json | ||
{ | ||
"title": "解决方案增强", | ||
"content": "经过审查,我注意到...", | ||
"next_action": "continue", | ||
} | ||
""", | ||
), | ||
( | ||
"最终审查员", | ||
""" | ||
你是最终审查员,确保解决方案的正确性、全面性以及表达的完善性。 | ||
**目标** | ||
- 进行全面的最终审查。 | ||
- 解决所有剩余问题。 | ||
- 提供最终优化后的解决方案。 | ||
- 反思并评估对答案的整体信心。 | ||
**指示** | ||
1. **最终审查** | ||
- 仔细阅读整个解决方案。 | ||
- 检查是否还有任何遗漏的错误或不一致之处。 | ||
2. **润色** | ||
- 改进解决方案的表达和清晰度。 | ||
- 确保逻辑流畅且易于阅读。 | ||
4. **反思和总结** | ||
- 反思解决方案的整体质量。 | ||
- 决定是否需要进一步优化。 | ||
5. **最终答案** | ||
- 提交最终优化后的解决方案。 | ||
**输出格式** | ||
请以JSON格式输出: | ||
- `"title"`: "最终优化解决方案" | ||
- `"content"`: 对最终审查和优化解决方案的详细解释。 | ||
- `"next_action"`: `"final_answer"` | ||
**示例响应:** | ||
```json | ||
{ | ||
"title": "最终优化解决方案", | ||
"content": 完整的优化后解决方案, | ||
"next_action": "final_answer" | ||
} | ||
""", | ||
), | ||
] | ||
|
||
def build_agents(self): | ||
agents = [] | ||
for name, prompt in self.agent_prompts: | ||
agents.append(Agent(name, prompt)) | ||
return agents |
This file contains 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,70 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from agent.agent_builder import AgentBuilder \n", | ||
"import os\n", | ||
"import time\n", | ||
"from openai import OpenAI\n", | ||
"from getpass import getpass\n", | ||
"\n", | ||
"os.environ[\"OPENAI_API_KEY\"] = getpass()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"query = input(\"请输入问题\")\n", | ||
"\n", | ||
"agent_bulider = AgentBuilder()\n", | ||
"agents = agent_bulider.build_agents() \n", | ||
"\n", | ||
"previous_solution = None\n", | ||
"all_solutions = []\n", | ||
"\n", | ||
"for agent in agents:\n", | ||
" solution = agent.get_solution(query, previous_solution)\n", | ||
" all_solutions.append((agent.name, solution))\n", | ||
" previous_solution = solution " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"for solution_temp in all_solutions:\n", | ||
" print(solution_temp[1])" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "langchain", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |