Skip to content
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

Refactor: magent-ui #98

Merged
merged 23 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"source.fixAll.eslint": "explicit"
}
},
"cSpell.words": ["magent", "uvicorn"],
"cSpell.words": ["llms", "magent", "qwen", "tongyi", "uvicorn"],
"python.analysis.typeCheckingMode": "basic",
"python.analysis.autoImportCompletions": true
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ PEER Multi-Agent Dialogue

#### Future Plans

- November
- [ ] Workflow nodes support code nodes
- [ ] Workflow nodes support intent recognition
- [ ] Clearer debugging information
- [ ] Workflow nodes support code nodes
- [ ] Workflow nodes support intent recognition
- [ ] Clearer debugging information

#### Configuration

Expand Down
12 changes: 7 additions & 5 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

## magent-ui

为本地代码研发提供辅助产品化能力,方便用户调试
为本地代码研发提供辅助产品化能力,方便用户调试。支持多种 Agent 研发框架。

- [agentUniverse](https://github.com/alipay/agentUniverse)
- [langchain](https://github.com/langchain-ai/langchain)

### agentUniverse

Expand All @@ -21,10 +24,9 @@ PEER 多智能体对话

#### 后续计划

- 11 月
- [ ] 工作流节点支持代码节点
- [ ] 工作流节点支持意图识别
- [ ] 更清晰的调试信息
- [ ] 工作流节点支持代码节点
- [ ] 工作流节点支持意图识别
- [ ] 更清晰的调试信息

#### 配置

Expand Down
3 changes: 0 additions & 3 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ dependencies = [
"pydantic-settings>=2.3.2",
"psycopg2-binary>=2.9.9",
"fastapi-pagination>=0.12.25",
"langchain>=0.1.20",
"langchain-community>=0.0.38",
"sse-starlette>=2.1.2",
"agentuniverse>=0.0.11",
"langchain_openai",
]
readme = "README.md"
Expand Down
11 changes: 11 additions & 0 deletions examples/langchain-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# python generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# venv
.venv

1 change: 1 addition & 0 deletions examples/langchain-example/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
1 change: 1 addition & 0 deletions examples/langchain-example/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include src/magent_ui/static *
1 change: 1 addition & 0 deletions examples/langchain-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# magent_ui
223 changes: 223 additions & 0 deletions examples/langchain-example/openai_agent.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "9bf3d1a7-bbbe-445c-b74d-cd31692ae30e",
"metadata": {
"execution": {
"shell.execute_reply.end": "2024-11-19T11:05:54.981070Z",
"shell.execute_reply.started": "2024-11-19T11:05:54.911264Z",
"to_execute": "2024-11-19T11:05:54.969Z"
},
"libroFormatter": "formatter-string"
},
"outputs": [
{
"data": {
"text/plain": [
"AgentExecutor(verbose=True, tags=['zero-shot-react-description'], agent=ZeroShotAgent(llm_chain=LLMChain(verbose=False, prompt=PromptTemplate(input_variables=['agent_scratchpad', 'input'], input_types={}, partial_variables={}, template='Answer the following questions as best you can. You have access to the following tools:\\n\\nhash_string(word: str) - This function to get the hash value of a word.\\n\\n:param input_string: The string to be hashed\\n:return: The hash value of the input string\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [hash_string]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: {input}\\nThought:{agent_scratchpad}'), llm=ChatOpenAI(client=<openai.resources.chat.completions.Completions object at 0x11ba8d460>, async_client=<openai.resources.chat.completions.AsyncCompletions object at 0x11ba12b20>, root_client=<openai.OpenAI object at 0x11ba59dc0>, root_async_client=<openai.AsyncOpenAI object at 0x11ba8d490>, model_name='gpt-4o', temperature=0.0, model_kwargs={}, openai_api_key=SecretStr('**********')), output_parser=StrOutputParser(), llm_kwargs={}), output_parser=MRKLOutputParser(), allowed_tools=['hash_string']), tools=[StructuredTool(name='hash_string', description='This function to get the hash value of a word.\\n\\n:param input_string: The string to be hashed\\n:return: The hash value of the input string', args_schema=<class 'langchain_core.utils.pydantic.hash_string'>, func=<function hash_string at 0x11ba78d30>)])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.tools import tool\n",
"from langchain.agents import initialize_agent\n",
"from langchain.agents import AgentType\n",
"\n",
"\n",
"@tool\n",
"def hash_string(word:str):\n",
" \"\"\"\n",
" This function to get the hash value of a word.\n",
"\n",
" :param input_string: The string to be hashed\n",
" :return: The hash value of the input string\n",
" \"\"\"\n",
" import hashlib\n",
" hash_object = hashlib.sha256()\n",
" hash_object.update(word.encode())\n",
" hash_value = hash_object.hexdigest()\n",
" return hash_value\n",
"\n",
"\n",
"llm = ChatOpenAI(\n",
" model=\"gpt-4o\",\n",
" temperature=0,\n",
" max_tokens=None,\n",
" timeout=None,\n",
" max_retries=2,\n",
")\n",
"\n",
"tools = [hash_string]\n",
"\n",
"agent = initialize_agent(\n",
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n",
"\n",
"agent\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5e16c965-cedc-45f8-ac41-d0111abf5b5d",
"metadata": {
"execution": {
"shell.execute_reply.end": "2024-11-19T11:05:56.472219Z",
"shell.execute_reply.started": "2024-11-19T11:05:56.466182Z",
"to_execute": "2024-11-19T11:05:56.523Z"
},
"libroFormatter": "formatter-string"
},
"outputs": [
{
"data": {
"text/plain": [
"ChatOpenAI(client=<openai.resources.chat.completions.Completions object at 0x11ba8d460>, async_client=<openai.resources.chat.completions.AsyncCompletions object at 0x11ba12b20>, root_client=<openai.OpenAI object at 0x11ba59dc0>, root_async_client=<openai.AsyncOpenAI object at 0x11ba8d490>, model_name='gpt-4o', temperature=0.0, model_kwargs={}, openai_api_key=SecretStr('**********'))"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.agent.llm_chain.llm"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b70afa86-39c9-4278-96de-da12dedb5ed5",
"metadata": {
"execution": {
"shell.execute_reply.end": "2024-11-19T11:05:59.907734Z",
"shell.execute_reply.started": "2024-11-19T11:05:57.155502Z",
"to_execute": "2024-11-19T11:05:57.193Z"
},
"libroFormatter": "formatter-string"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new None chain...\u001b[0m\n",
"{'actions': [AgentAction(tool='hash_string', tool_input='Hello, how are you today?', log='The word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\\nAction: hash_string\\nAction Input: \"Hello, how are you today?\"')], 'messages': [AIMessage(content='The word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\\nAction: hash_string\\nAction Input: \"Hello, how are you today?\"', additional_kwargs={}, response_metadata={})]}\n",
"<class 'langchain_core.runnables.utils.AddableDict'>\n",
"--- False\n",
"--- False\n",
"--- False\n",
"--- False\n",
"\u001b[32;1m\u001b[1;3mThe word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\n",
"Action: hash_string\n",
"Action Input: \"Hello, how are you today?\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3m6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48\u001b[0m\n",
"Thought:{'steps': [AgentStep(action=AgentAction(tool='hash_string', tool_input='Hello, how are you today?', log='The word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\\nAction: hash_string\\nAction Input: \"Hello, how are you today?\"'), observation='6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48')], 'messages': [HumanMessage(content='6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48', additional_kwargs={}, response_metadata={})]}\n",
"<class 'langchain_core.runnables.utils.AddableDict'>\n",
"--- False\n",
"--- False\n",
"--- False\n",
"--- False\n",
"\u001b[32;1m\u001b[1;3mI now know the final answer.\n",
"Final Answer: The hash value of the sentence \"Hello, how are you today?\" is 6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"{'output': 'The hash value of the sentence \"Hello, how are you today?\" is 6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48.', 'messages': [AIMessage(content='I now know the final answer.\\nFinal Answer: The hash value of the sentence \"Hello, how are you today?\" is 6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48.', additional_kwargs={}, response_metadata={})]}\n",
"<class 'langchain_core.runnables.utils.AddableDict'>\n",
"--- False\n",
"--- False\n",
"--- False\n",
"--- False\n"
]
},
{
"data": {
"text/plain": [
"{'actions': [AgentAction(tool='hash_string', tool_input='Hello, how are you today?', log='The word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\\nAction: hash_string\\nAction Input: \"Hello, how are you today?\"')],\n",
" 'messages': [AIMessage(content='The word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\\nAction: hash_string\\nAction Input: \"Hello, how are you today?\"', additional_kwargs={}, response_metadata={}),\n",
" HumanMessage(content='6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48', additional_kwargs={}, response_metadata={}),\n",
" AIMessage(content='I now know the final answer.\\nFinal Answer: The hash value of the sentence \"Hello, how are you today?\" is 6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48.', additional_kwargs={}, response_metadata={})],\n",
" 'steps': [AgentStep(action=AgentAction(tool='hash_string', tool_input='Hello, how are you today?', log='The word \"你好\" is Chinese and translates to \"Hello\" in English. I will now create a sentence starting with \"Hello\" and then get the hash value of the result.\\nAction: hash_string\\nAction Input: \"Hello, how are you today?\"'), observation='6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48')],\n",
" 'output': 'The hash value of the sentence \"Hello, how are you today?\" is 6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48.'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"msg_iterator=agent.stream('Translate the follow word to English, and then write a sentence starting with the word, and then get the hash value of the result, \"你好\"')\n",
"\n",
"from langchain_core.agents import AgentAction, AgentStep, AgentFinish\n",
"from langchain_core.messages import BaseMessageChunk\n",
"\n",
"first = True\n",
"gathered = None\n",
"for chunk in msg_iterator:\n",
" print(chunk)\n",
" print(type(chunk))\n",
" print('---', isinstance(chunk, BaseMessageChunk))\n",
" print('---', isinstance(chunk, AgentAction))\n",
" print('---', isinstance(chunk, AgentStep))\n",
" print('---', isinstance(chunk, AgentFinish))\n",
" \n",
" if first:\n",
" gathered = chunk\n",
" first = False\n",
" else:\n",
" gathered = gathered + chunk\n",
"gathered\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "c40a71ab-e0d4-4dd1-9fc5-7458eddf1d46",
"metadata": {
"execution": {
"shell.execute_reply.end": "2024-11-19T16:50:27.226125Z",
"shell.execute_reply.started": "2024-11-19T16:50:27.221123Z",
"to_execute": "2024-11-19T16:50:27.275Z"
},
"libroFormatter": "formatter-string"
},
"outputs": [
{
"data": {
"text/plain": [
"'6e766a49e512e0ba0bc935e2aacd3e5a4a34add17f83afc4c9e669c70241cd48'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gathered.get('steps')[0].observation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "884e869b-8c1f-40f2-8bc6-23b3a29ab545",
"metadata": {
"libroFormatter": "formatter-string"
},
"outputs": [],
"source": []
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading