-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: update dependencies * refactor(langchain): core adaptor * feat(example): langchain example * chore: spell * feat(langchain): handle ai message * feat(chat): add rehype raw plugin * feat(langchain): input & output format * feat(example): interperter example * fix: chat bot width * fix: modify the presentation of the agent execution process * feat: launch agent in notebook and display qrcode * fix: add qrcode * docs: tongyi agent demo * fix: mobile view change * chore(langchain): update environment & version * chore(flow): tailwind auto update * docs: readme * chore: fix config * feat: magent-ui-core * refactor: magent-ui-core * fix: lint * docs: update readme * feat(core): magent-core --------- Co-authored-by: xujingli.xjl <[email protected]>
- Loading branch information
Showing
68 changed files
with
3,035 additions
and
986 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
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
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
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
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,11 @@ | ||
# python generated files | ||
__pycache__/ | ||
*.py[oc] | ||
build/ | ||
dist/ | ||
wheels/ | ||
*.egg-info | ||
|
||
# venv | ||
.venv | ||
|
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 @@ | ||
3.10 |
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 @@ | ||
recursive-include src/magent_ui/static * |
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 @@ | ||
# magent_ui |
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,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 | ||
} |
Oops, something went wrong.