Files
langgraph/examples/langgraph.ipynb
T
2024-01-05 16:01:08 -08:00

108 lines
3.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "53b9dce4-e4ae-4bdb-b752-0f04350a2e3d",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.agents import create_openai_functions_agent\n",
"from langchain_core.prompts import PromptTemplate\n",
"from langchain import hub\n",
"from langchain.agents import AgentExecutor, create_openai_functions_agent\n",
"from langchain_community.chat_models import ChatOpenAI\n",
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
"from langchain_core.runnables import RunnablePassthrough, RunnableLambda\n",
"from permchain.langgraph import Actor, DecisionPoint, Graph\n",
"\n",
"tools = [TavilySearchResults(max_results=1)]\n",
"\n",
"# Get the prompt to use - you can modify this!\n",
"prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",
"\n",
"# Choose the LLM that will drive the agent\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")\n",
"\n",
"# Construct the OpenAI Functions agent\n",
"agent_runnable = create_openai_functions_agent(llm, tools, prompt)\n",
"\n",
"from langchain_core.agents import AgentFinish\n",
"# Define decision-making logic\n",
"def should_continue(data):\n",
" # Logic to decide whether to continue in the loop or exit\n",
" if isinstance(data['agent_outcome'], AgentFinish):\n",
" return \"exit\"\n",
" else:\n",
" return \"continue\"\n",
" \n",
"def execute_tools(data):\n",
" agent_action = data.pop('agent_outcome')\n",
" observation = {t.name: t for t in tools}[agent_action.tool].invoke(agent_action.tool_input)\n",
" data['intermediate_steps'].append((agent_action, observation))\n",
" return data\n",
" \n",
" \n",
"\n",
"# Define agents\n",
"agent = RunnablePassthrough.assign(\n",
" agent_outcome = agent_runnable\n",
")\n",
"\n",
"# Define a new graph\n",
"workflow = Graph()\n",
"decision_point = DecisionPoint(\"exit?\", should_continue)\n",
"llm_agent = Actor(\"agent\", agent)\n",
"tool_actor = Actor(\"tools\", RunnableLambda(execute_tools))\n",
"\n",
"# Register actors\n",
"workflow.register(llm_agent)\n",
"workflow.register(tool_actor)\n",
"workflow.register(decision_point)\n",
"\n",
"# Define connections with conditional logic\n",
"workflow.connect(llm_agent, decision_point)\n",
"workflow.branch(decision_point, tool_actor, condition=\"continue\")\n",
"workflow.branch(decision_point, None, condition=\"exit\") # Exit the workflow\n",
"workflow.connect(tool_actor, llm_agent)\n",
"\n",
"# Define entry point and execute the graph\n",
"workflow.set_entry_point(llm_agent)\n",
"chain = workflow.compile()\n",
"\n",
"chain.invoke({\"input\": \"what is the weather in sf\", \"intermediate_steps\": []})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2515edbc-a9b6-42ce-bb5e-f1f2503d1bb4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}