Skip to content

Langchain #92

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,10 @@ openai_api/data/fine_food_reviews_with_embeddings_1k_2146.csv
*.bin

langchain/openai-translator/flagged/*
langchain/openai-translator/flask_temps/*
langchain/openai-translator/flask_temps/*
openai-translator/ai_translator/.idea/ai_translator.iml
openai-translator/ai_translator/.idea/inspectionProfiles/profiles_settings.xml
openai-translator/ai_translator/.idea/inspectionProfiles/Project_Default.xml
openai-translator/ai_translator/.idea/misc.xml
openai-translator/ai_translator/.idea/modules.xml
openai-translator/ai_translator/.idea/vcs.xml
313 changes: 313 additions & 0 deletions langchain/jupyter/agents/openai_functionTest.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-03-29T12:01:26.511156800Z",
"start_time": "2024-03-29T12:01:20.975005400Z"
}
},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(temperature=0)"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from langchain.agents import tool\n",
"\n",
"@tool\n",
"def get_word_length(word: str) -> int:\n",
" \"\"\"Returns the length of a word.\"\"\"\n",
" return len(word)\n",
"\n",
"tools = [get_word_length]"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:02:39.804177100Z",
"start_time": "2024-03-29T12:02:38.049455200Z"
}
},
"id": "2d9fe2c1a3654657",
"execution_count": 2
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from langchain.schema import SystemMessage\n",
"from langchain.agents import OpenAIFunctionsAgent\n",
"\n",
"system_message = SystemMessage(content=\"你是非常强大的AI助手,但在计算单词长度方面不擅长。\")\n",
"prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:04:19.417806500Z",
"start_time": "2024-03-29T12:04:19.341513200Z"
}
},
"id": "54350a24c993624d",
"execution_count": 3
},
{
"cell_type": "code",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\soft\\Python312\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The class `langchain.agents.openai_functions_agent.base.OpenAIFunctionsAgent` was deprecated in langchain 0.1.0 and will be removed in 0.2.0. Use create_openai_functions_agent instead.\n",
" warn_deprecated(\n"
]
}
],
"source": [
"agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:04:53.611433300Z",
"start_time": "2024-03-29T12:04:52.422558700Z"
}
},
"id": "7e3e7c9a1e101a69",
"execution_count": 4
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from langchain.agents import AgentExecutor\n",
"\n",
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:05:42.170247400Z",
"start_time": "2024-03-29T12:05:42.150934200Z"
}
},
"id": "17922ddf5f435a1d",
"execution_count": 5
},
{
"cell_type": "code",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\soft\\Python312\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
" warn_deprecated(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
"\u001B[32;1m\u001B[1;3m\n",
"Invoking: `get_word_length` with `{'word': 'educa'}`\n",
"\n",
"\n",
"\u001B[0m\u001B[36;1m\u001B[1;3m5\u001B[0m\u001B[32;1m\u001B[1;3m单词“educa”中有5个字母。\u001B[0m\n",
"\n",
"\u001B[1m> Finished chain.\u001B[0m\n"
]
},
{
"data": {
"text/plain": "'单词“educa”中有5个字母。'"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.run(\"单词“educa”中有多少个字母?\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:06:13.829616200Z",
"start_time": "2024-03-29T12:06:11.478938400Z"
}
},
"id": "bc07104a44ab6d5b",
"execution_count": 6
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from langchain.prompts import MessagesPlaceholder\n",
"\n",
"MEMORY_KEY = \"chat_history\"\n",
"prompt = OpenAIFunctionsAgent.create_prompt(\n",
" system_message=system_message,\n",
" extra_prompt_messages=[MessagesPlaceholder(variable_name=MEMORY_KEY)]\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:07:55.933638900Z",
"start_time": "2024-03-29T12:07:55.593759400Z"
}
},
"id": "ccb7cece70540a30",
"execution_count": 7
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from langchain.memory import ConversationBufferMemory\n",
"\n",
"memory = ConversationBufferMemory(memory_key=MEMORY_KEY, return_messages=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:08:53.107717300Z",
"start_time": "2024-03-29T12:08:53.102043400Z"
}
},
"id": "addb142685d40adb",
"execution_count": 8
},
{
"cell_type": "code",
"outputs": [],
"source": [
"agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)\n",
"agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:09:50.125508800Z",
"start_time": "2024-03-29T12:09:50.116425500Z"
}
},
"id": "dce86e5dc17f716f",
"execution_count": 9
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
"\u001B[32;1m\u001B[1;3m\n",
"Invoking: `get_word_length` with `{'word': 'educa'}`\n",
"\n",
"\n",
"\u001B[0m\u001B[36;1m\u001B[1;3m5\u001B[0m\u001B[32;1m\u001B[1;3m单词“educa”中有5个字母。\u001B[0m\n",
"\n",
"\u001B[1m> Finished chain.\u001B[0m\n"
]
},
{
"data": {
"text/plain": "'单词“educa”中有5个字母。'"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.run(\"单词“educa”中有多少个字母?\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:10:15.044103900Z",
"start_time": "2024-03-29T12:10:12.383340Z"
}
},
"id": "f4a142b8bb0cbc0d",
"execution_count": 10
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
"\u001B[32;1m\u001B[1;3m抱歉,我之前给出的答案有误。单词“educa”实际上不是一个真实的单词。请问您还有其他问题吗?\u001B[0m\n",
"\n",
"\u001B[1m> Finished chain.\u001B[0m\n"
]
},
{
"data": {
"text/plain": "'抱歉,我之前给出的答案有误。单词“educa”实际上不是一个真实的单词。请问您还有其他问题吗?'"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.run(\"那是一个真实的单词吗?\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-03-29T12:10:45.587808900Z",
"start_time": "2024-03-29T12:10:43.443403100Z"
}
},
"id": "6fe525108f45b5da",
"execution_count": 11
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading