mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
Update info seeking (#592)
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 1,
|
||||
"id": "53216ab5-2cd3-48a4-8778-41ba10f72519",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -41,7 +41,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 2,
|
||||
"id": "5f795b78-004d-40ca-95d6-069f67e4f9c9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -57,26 +57,9 @@
|
||||
"\n",
|
||||
"If you are not able to discerne this info, ask them to clarify! Do not attempt to wildly guess.\n",
|
||||
"\n",
|
||||
"After you are able to discerne all the information, call the relevant tool\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "153a7c89-ec75-430e-96ac-6294d4e6ea2b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = ChatOpenAI(temperature=0)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "8325ed0e-177a-4129-b4a4-cd54d527c634",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"After you are able to discerne all the information, call the relevant tool\"\"\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_messages_info(messages):\n",
|
||||
" return [SystemMessage(content=template)] + messages\n",
|
||||
"\n",
|
||||
@@ -87,16 +70,10 @@
|
||||
" objective: str\n",
|
||||
" variables: List[str]\n",
|
||||
" constraints: List[str]\n",
|
||||
" requirements: List[str]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "95bb557a-47f2-46fd-adf7-9cf0e8ffe51c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
" requirements: List[str]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"llm = ChatOpenAI(temperature=0)\n",
|
||||
"llm_with_tool = llm.bind_tools([PromptInstructions])\n",
|
||||
"\n",
|
||||
"chain = get_messages_info | llm_with_tool"
|
||||
@@ -115,19 +92,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "5b6736fb-21d7-44d3-a3fe-460f15945654",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Helper function for determining if tool was called\n",
|
||||
"def _is_tool_call(msg):\n",
|
||||
" return hasattr(msg, \"additional_kwargs\") and \"tool_calls\" in msg.additional_kwargs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 3,
|
||||
"id": "ca9a0234-bbeb-4bff-8276-8dde499c3390",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -140,24 +105,17 @@
|
||||
"\n",
|
||||
"# Function to get the messages for the prompt\n",
|
||||
"# Will only get messages AFTER the tool call\n",
|
||||
"def get_prompt_messages(messages):\n",
|
||||
"def get_prompt_messages(messages: list):\n",
|
||||
" tool_call = None\n",
|
||||
" other_msgs = []\n",
|
||||
" for m in messages:\n",
|
||||
" if _is_tool_call(m):\n",
|
||||
" tool_call = m.additional_kwargs[\"tool_calls\"][0][\"function\"][\"arguments\"]\n",
|
||||
" if getattr(m, 'tool_calls', None):\n",
|
||||
" tool_call = m.tool_calls[0][\"args\"]\n",
|
||||
" elif tool_call is not None:\n",
|
||||
" other_msgs.append(m)\n",
|
||||
" return [SystemMessage(content=prompt_system.format(reqs=tool_call))] + other_msgs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3293d2d3-e060-4c98-8065-3696b049b5fc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
" return [SystemMessage(content=prompt_system.format(reqs=tool_call))] + other_msgs\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"prompt_gen_chain = get_prompt_messages | llm"
|
||||
]
|
||||
},
|
||||
@@ -177,18 +135,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 4,
|
||||
"id": "74f29e15-20e2-420c-a450-84e929f16e4e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph.graph import END\n",
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"def get_state(messages):\n",
|
||||
" if _is_tool_call(messages[-1]):\n",
|
||||
" if getattr(messages[-1], 'tool_calls', None):\n",
|
||||
" return \"prompt\"\n",
|
||||
" elif not isinstance(messages[-1], HumanMessage):\n",
|
||||
" return END\n",
|
||||
" for m in messages:\n",
|
||||
" if _is_tool_call(m):\n",
|
||||
" if getattr(m, \"tool_calls\", None):\n",
|
||||
" return \"prompt\"\n",
|
||||
" return \"info\""
|
||||
]
|
||||
@@ -206,12 +167,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 5,
|
||||
"id": "59d9d6b4-dce4-43cc-9a1a-61a7912ed5b8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph.graph import MessageGraph, END\n",
|
||||
"from langgraph.graph import MessageGraph, START\n",
|
||||
"from langgraph.checkpoint.sqlite import SqliteSaver\n",
|
||||
"\n",
|
||||
"memory = SqliteSaver.from_conn_string(\":memory:\")\n",
|
||||
@@ -222,7 +183,7 @@
|
||||
"workflow.add_node(\"prompt\", prompt_gen_chain)\n",
|
||||
"workflow.add_conditional_edges(\"info\", get_state, nodes)\n",
|
||||
"workflow.add_conditional_edges(\"prompt\", get_state, nodes)\n",
|
||||
"workflow.set_entry_point(\"info\")\n",
|
||||
"workflow.add_edge(START, \"info\")\n",
|
||||
"graph = workflow.compile(checkpointer=memory)"
|
||||
]
|
||||
},
|
||||
@@ -238,160 +199,44 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"id": "25793988-45a2-4e65-b33c-64e72aadb10e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): hi!\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content='Hello! How can I assist you today?'\n",
|
||||
"content='Hello! How can I assist you today?' response_metadata={'token_usage': {'completion_tokens': 10, 'prompt_tokens': 170, 'total_tokens': 180}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-82cac254-ed31-40c7-8155-9de6596ab47f-0' usage_metadata={'input_tokens': 170, 'output_tokens': 10, 'total_tokens': 180}\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): build me a prompt for extraction\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content='Sure! I can help you with that. Could you please provide me with more details about the prompt you want to create? Specifically, I need to know the objective of the prompt, the variables that will be passed into the prompt template, any constraints for what the output should not do, and any requirements that the output must adhere to.'\n",
|
||||
"content='Sure! I can help you with that. To create an extraction prompt, I need some information from you. \\n\\nCan you please provide the following details:\\n1. What is the objective of the prompt?\\n2. What variables will be passed into the prompt template?\\n3. Any constraints for what the output should NOT do?\\n4. Any requirements that the output MUST adhere to?\\n\\nOnce I have this information, I can assist you in creating the extraction prompt template.' response_metadata={'token_usage': {'completion_tokens': 94, 'prompt_tokens': 193, 'total_tokens': 287}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-b66dbf2e-de98-4022-891e-2f6c8e8676f6-0' usage_metadata={'input_tokens': 193, 'output_tokens': 94, 'total_tokens': 287}\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): i want to do extraction over a page\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content='Great! Could you please provide me with more details about the objective of the extraction? What specific information are you looking to extract from the page?'\n",
|
||||
"content='Great! Thank you for providing the objective of the prompt. \\n\\nNow, could you please specify the variables that will be passed into the prompt template for populating the CSAT record in your CRM? \\n\\nAdditionally, are there any constraints for what the output should NOT do and any requirements that the output MUST adhere to? \\n\\nOnce I have this information, I can help you create the extraction prompt template for populating the CSAT record in your CRM.' response_metadata={'token_usage': {'completion_tokens': 92, 'prompt_tokens': 306, 'total_tokens': 398}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-26e74d73-5d7c-4731-b2fc-f345cdf81bba-0' usage_metadata={'input_tokens': 306, 'output_tokens': 92, 'total_tokens': 398}\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): i want the user to specify that at run time\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content=\"Understood. So the objective of the prompt is to allow the user to specify the information they want to extract from a page at runtime. \\n\\nNow, let's move on to the variables. Are there any specific variables that you would like to pass into the prompt template? For example, the URL of the page or any other parameters that might be relevant for the extraction process.\"\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): the schema to extract, and the text to extract it from\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content='Got it. So the variables that will be passed into the prompt template are the schema to extract and the text to extract it from.\\n\\nNext, are there any constraints for what the output should not do? For example, should the output not include any sensitive information or should it not exceed a certain length?'\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): it must be in json\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content='Understood. So a requirement for the output is that it must be in JSON format.\\n\\nLastly, are there any specific requirements that the output must adhere to? For example, should the output follow a specific structure or include certain fields?'\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): must be json, must include the same fields as the schema specified\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Output from node 'info':\n",
|
||||
"---\n",
|
||||
"content='Got it. So the requirements for the output are that it must be in JSON format and it must include the same fields as the schema specified.\\n\\nBased on the information you provided, I will now generate the prompt template for extraction. Please give me a moment.\\n\\n' additional_kwargs={'tool_calls': [{'id': 'call_6roy9dQoIrQZsHffR9kjAr0e', 'function': {'arguments': '{\\n \"objective\": \"Extract specific information from a page\",\\n \"variables\": [\"schema\", \"text\"],\\n \"constraints\": [\"Output should not include sensitive information\", \"Output should not exceed a certain length\"],\\n \"requirements\": [\"Output must be in JSON format\", \"Output must include the same fields as the specified schema\"]\\n}', 'name': 'PromptInstructions'}, 'type': 'function'}]}\n",
|
||||
"content='' additional_kwargs={'tool_calls': [{'id': 'call_hjFAEvsaJhpeJ5001ZdCEyst', 'function': {'arguments': '{\"objective\":\"Create an extraction prompt template to populate a CSAT record in a CRM\",\"variables\":[\"comment\",\"score\"],\"constraints\":[\"Output should be valid JSON\",\"Output should reflect/summarize the user\\'s inputs\"],\"requirements\":[\"Output must adhere to the specified variables\"]}', 'name': 'PromptInstructions'}, 'type': 'function'}]} response_metadata={'token_usage': {'completion_tokens': 64, 'prompt_tokens': 432, 'total_tokens': 496}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None} id='run-38042cdf-8574-4565-bb39-419ee777819d-0' tool_calls=[{'name': 'PromptInstructions', 'args': {'objective': 'Create an extraction prompt template to populate a CSAT record in a CRM', 'variables': ['comment', 'score'], 'constraints': ['Output should be valid JSON', \"Output should reflect/summarize the user's inputs\"], 'requirements': ['Output must adhere to the specified variables']}, 'id': 'call_hjFAEvsaJhpeJ5001ZdCEyst'}] usage_metadata={'input_tokens': 432, 'output_tokens': 64, 'total_tokens': 496}\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"Output from node 'prompt':\n",
|
||||
"---\n",
|
||||
"content='Extract specific information from a page and output the result in JSON format. The input page should contain the following fields: {{schema}}. The extracted information should be stored in the variable {{text}}. Ensure that the output does not include any sensitive information and does not exceed a certain length. Additionally, the output should include the same fields as the specified schema.'\n",
|
||||
"content='## CSAT Record Extraction Prompt Template\\n\\n### Objective:\\nCreate an extraction prompt template to populate a CSAT record in a CRM.\\n\\n### Variables:\\n1. `comment`: (string) The feedback comment provided by the customer.\\n2. `score`: (integer) The satisfaction score given by the customer.\\n\\n### Constraints:\\n- Output should be valid JSON.\\n- Output should reflect/summarize the user\\'s inputs.\\n\\n### Requirements:\\nOutput must adhere to the specified variables.\\n\\n#### Prompt Template:\\n```json\\n{\\n \"comment\": \"Please provide your feedback here.\",\\n \"score\": \"On a scale of 1-10, how satisfied are you with our service?\"\\n}\\n```' response_metadata={'token_usage': {'completion_tokens': 138, 'prompt_tokens': 82, 'total_tokens': 220}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-01ae6136-5c1e-41f8-9746-93304dfd489d-0' usage_metadata={'input_tokens': 82, 'output_tokens': 138, 'total_tokens': 220}\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"User (q/Q to quit): q\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"AI: Byebye\n"
|
||||
]
|
||||
}
|
||||
@@ -407,8 +252,6 @@
|
||||
" print(\"AI: Byebye\")\n",
|
||||
" break\n",
|
||||
" for output in graph.stream([HumanMessage(content=user)], config=config):\n",
|
||||
" if \"__end__\" in output:\n",
|
||||
" continue\n",
|
||||
" # stream() yields dictionaries with output keyed by node name\n",
|
||||
" for key, value in output.items():\n",
|
||||
" print(f\"Output from node '{key}':\")\n",
|
||||
@@ -442,7 +285,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.1"
|
||||
"version": "3.12.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user