Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rlancemartin committed Apr 4, 2024
1 parent 7aa32bb commit dfa461b
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions cookbook/anthropic_structured_outputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"metadata": {},
"outputs": [],
"source": [
"! pip install langchain-anthropic"
"! pip install -U langchain-anthropic"
]
},
{
Expand All @@ -31,8 +31,8 @@
"source": [
"# Optional\n",
"import os\n",
"#os.environ['LANGCHAIN_TRACING_V2'] = 'true' # enables tracing \n",
"#os.environ['LANGCHAIN_API_KEY'] = <your-api-key>"
"# os.environ['LANGCHAIN_TRACING_V2'] = 'true' # enables tracing\n",
"# os.environ['LANGCHAIN_API_KEY'] = <your-api-key>"
]
},
{
Expand All @@ -58,32 +58,36 @@
},
{
"cell_type": "code",
"execution_count": 196,
"id": "8f6e09a2-33bb-407f-8ea4-524f002c775a",
"execution_count": null,
"id": "9caa2aaf-1918-4a8a-982d-f8052b92ed44",
"metadata": {},
"outputs": [],
"source": [
"from langchain_anthropic import ChatAnthropic\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"\n",
"# Data model \n",
"\n",
"# Data model\n",
"class code(BaseModel):\n",
" \"\"\"Code output\"\"\"\n",
"\n",
" prefix: str = Field(description=\"Description of the problem and approach\")\n",
" imports: str = Field(description=\"Code block import statements\")\n",
" code: str = Field(description=\"Code block not including import statements\")\n",
"\n",
"\n",
"# LLM\n",
"llm = ChatAnthropic(\n",
" model=\"claude-3-opus-20240229\",\n",
" default_headers={\"anthropic-beta\": \"tools-2024-04-04\"}\n",
" default_headers={\"anthropic-beta\": \"tools-2024-04-04\"},\n",
")\n",
"\n",
"# Structured output, including raw will capture raw output and parser errors\n",
"structured_llm = llm.with_structured_output(code,include_raw=True) \n",
"code_output = structured_llm.invoke(\"Write a python program that prints the string 'hello world' and tell me how it works in a sentence\")"
"structured_llm = llm.with_structured_output(code, include_raw=True)\n",
"code_output = structured_llm.invoke(\n",
" \"Write a python program that prints the string 'hello world' and tell me how it works in a sentence\"\n",
")"
]
},
{
Expand All @@ -106,7 +110,7 @@
],
"source": [
"# Initial reasoning stage\n",
"code_output['raw'].content[0]"
"code_output[\"raw\"].content[0]"
]
},
{
Expand Down Expand Up @@ -134,7 +138,7 @@
],
"source": [
"# Tool call\n",
"code_output['raw'].content[1]"
"code_output[\"raw\"].content[1]"
]
},
{
Expand All @@ -158,7 +162,7 @@
],
"source": [
"# JSON str\n",
"code_output['raw'].content[1]['input']"
"code_output[\"raw\"].content[1][\"input\"]"
]
},
{
Expand All @@ -169,7 +173,7 @@
"outputs": [],
"source": [
"# Error\n",
"error = code_output['parsing_error']\n",
"error = code_output[\"parsing_error\"]\n",
"error"
]
},
Expand All @@ -181,7 +185,7 @@
"outputs": [],
"source": [
"# Result\n",
"parsed_result = code_output['parsed']"
"parsed_result = code_output[\"parsed\"]"
]
},
{
Expand Down Expand Up @@ -316,17 +320,23 @@
"metadata": {},
"outputs": [],
"source": [
" # Code solution prompt\n",
"# Code solution prompt\n",
"code_gen_prompt = ChatPromptTemplate.from_messages(\n",
" [(\"system\",\"\"\"<instructions> You are a coding assistant with expertise in LCEL, LangChain expression language. \\n \n",
" [\n",
" (\n",
" \"system\",\n",
" \"\"\"<instructions> You are a coding assistant with expertise in LCEL, LangChain expression language. \\n \n",
" Here is the LCEL documentation: \\n ------- \\n {context} \\n ------- \\n Answer the user question based on the \\n \n",
" above provided documentation. Ensure any code you provide can be executed with all required imports and variables \\n\n",
" defined. Structure your answer: 1) a prefix describing the code solution, 2) the imports, 3) the functioning code block. \\n\n",
" Invoke the code tool to structure the output correctly. </instructions> \\n Here is the user question:\"\"\"),\n",
" (\"placeholder\", \"{messages}\")]\n",
" Invoke the code tool to structure the output correctly. </instructions> \\n Here is the user question:\"\"\",\n",
" ),\n",
" (\"placeholder\", \"{messages}\"),\n",
" ]\n",
")\n",
"\n",
"# Data model \n",
"\n",
"# Data model\n",
"class code(BaseModel):\n",
" \"\"\"Code output\"\"\"\n",
"\n",
Expand All @@ -335,28 +345,32 @@
" code: str = Field(description=\"Code block not including import statements\")\n",
" description = \"Schema for code solutions to questions about LCEL.\"\n",
"\n",
"\n",
"# LLM\n",
"llm = ChatAnthropic(\n",
" model=\"claude-3-opus-20240229\",\n",
" default_headers={\"anthropic-beta\": \"tools-2024-04-04\"}\n",
" default_headers={\"anthropic-beta\": \"tools-2024-04-04\"},\n",
")\n",
"\n",
"# Structured output\n",
"# Include raw will capture raw output and parser errors\n",
"structured_llm = llm.with_structured_output(code,include_raw=True) \n",
"structured_llm = llm.with_structured_output(code, include_raw=True)\n",
"\n",
"\n",
"# Check for errors\n",
"def check(tool_output):\n",
" \"\"\"Check for errors\"\"\"\n",
" if tool_output['parsing_error']:\n",
" \n",
" if tool_output[\"parsing_error\"]:\n",
" # Report back output and parsing errors\n",
" raw_output = str(code_output['raw'].content)\n",
" error = tool_output['parsing_error']\n",
" raise ValueError(f\"Error parsing your output! Be sure to invoke the tool. Output: {raw_output}. \\n Parse error: {error}\")\n",
" raw_output = str(code_output[\"raw\"].content)\n",
" error = tool_output[\"parsing_error\"]\n",
" raise ValueError(\n",
" f\"Error parsing your output! Be sure to invoke the tool. Output: {raw_output}. \\n Parse error: {error}\"\n",
" )\n",
"\n",
" return tool_output\n",
"\n",
"\n",
"# Chain with output check\n",
"code_chain = code_gen_prompt | structured_llm | check"
]
Expand Down Expand Up @@ -391,10 +405,13 @@
" \"question\": inputs[\"question\"],\n",
" }\n",
"\n",
"\n",
"# This will be run as a fallback chain\n",
"fallback_chain = insert_errors | chain\n",
"N = 3 # Max re-tries\n",
"code_chain_re_try = code_chain.with_fallbacks(fallbacks=[fallback_chain] * N, exception_key=\"error\")"
"fallback_chain = insert_errors | code_chain\n",
"N = 3 # Max re-tries\n",
"code_chain_re_try = code_chain.with_fallbacks(\n",
" fallbacks=[fallback_chain] * N, exception_key=\"error\"\n",
")"
]
},
{
Expand All @@ -405,8 +422,10 @@
"outputs": [],
"source": [
"# Test\n",
"messages = [(\"user\",\"Write a RAG chain in LCEL.\")]\n",
"code_output_lcel = code_chain_re_try.invoke({\"context\": concatenated_content, \"messages\" : messages})"
"messages = [(\"user\", \"Write a RAG chain in LCEL.\")]\n",
"code_output_lcel = code_chain_re_try.invoke(\n",
" {\"context\": concatenated_content, \"messages\": messages}\n",
")"
]
},
{
Expand All @@ -416,7 +435,7 @@
"metadata": {},
"outputs": [],
"source": [
"parsed_result_lcel = code_output_lcel['parsed']"
"parsed_result_lcel = code_output_lcel[\"parsed\"]"
]
},
{
Expand Down

0 comments on commit dfa461b

Please sign in to comment.