"{'text': \"<thinking>\\n- To answer this request, we need to use the `code` function, which takes 3 required parameters: `prefix`, `imports`, and `code`.\\n- The user has provided enough context to generate the full code snippet and explanation, so we have values for all the required parameters:\\n - `prefix`: A brief description of what the code does\\n - `imports`: No imports needed for this simple program \\n - `code`: A Python print statement to print 'hello world'\\n</thinking>\",\n",
"{'text': \"<thinking>\\nThe tool 'code' is relevant for writing a Python program to print a string.\\n\\nTo use the 'code' tool, I need values for these required parameters:\\nprefix: A description of the problem and approach. I can provide this based on the request.\\nimports: The import statements needed for the code. For this simple program, no imports are needed, so I can leave this blank.\\ncode: The actual Python code, not including imports. I can write a simple print statement to output the string.\\n\\nI have all the required parameters, so I can proceed with calling the 'code' tool.\\n</thinking>\",\n",
" 'type': 'text'}"
]
},
"execution_count": 197,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@ -115,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": 198,
"execution_count": 3,
"id": "2393d9b6-67a2-41ea-ac01-dc038b4800f5",
"metadata": {},
"outputs": [
@ -124,14 +126,14 @@
"text/plain": [
"{'text': None,\n",
" 'type': 'tool_use',\n",
" 'id': 'toolu_012Dsij8yn87daCuGPpgkwas',\n",
" 'id': 'toolu_01UwZVQub6vL36wiBww6CU7a',\n",
" 'name': 'code',\n",
" 'input': {'prefix': \"This Python program prints the string 'hello world' to the console.\",\n",
" 'input': {'prefix': \"To print the string 'hello world' in Python:\",\n",
" 'imports': '',\n",
" 'code': \"print('hello world')\"}}"
]
},
"execution_count": 198,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@ -143,19 +145,19 @@
},
{
"cell_type": "code",
"execution_count": 199,
"execution_count": 4,
"id": "f4f390ac-fbda-4173-892a-ffd12844228c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'prefix': \"This Python program prints the string 'hello world' to the console.\",\n",
"{'prefix': \"To print the string 'hello world' in Python:\",\n",
" 'imports': '',\n",
" 'code': \"print('hello world')\"}"
]
},
"execution_count": 199,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@ -167,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 200,
"execution_count": 5,
"id": "ba77d0f8-f79b-4656-9023-085ffdaf35f5",
"metadata": {},
"outputs": [],
@ -179,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 201,
"execution_count": 6,
"id": "cd854451-68d7-43df-bcae-4f3c3565536a",
"metadata": {},
"outputs": [],
@ -190,17 +192,17 @@
},
{
"cell_type": "code",
"execution_count": 202,
"execution_count": 7,
"id": "47b3405f-0aea-460e-8603-f6092019fcd4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"This Python program prints the string 'hello world' to the console.\""
"\"To print the string 'hello world' in Python:\""
]
},
"execution_count": 202,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@ -211,7 +213,7 @@
},
{
"cell_type": "code",
"execution_count": 203,
"execution_count": 8,
"id": "85b16b62-1b72-4b6e-81fa-b1d707b728fa",
"metadata": {},
"outputs": [
@ -221,7 +223,7 @@
"''"
]
},
"execution_count": 203,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@ -232,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": 204,
"execution_count": 9,
"id": "23857441-3e67-460c-b6be-b57cf0dd17ad",
"metadata": {},
"outputs": [
@ -242,7 +244,7 @@
"\"print('hello world')\""
]
},
"execution_count": 204,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@ -278,7 +280,7 @@
},
{
"cell_type": "code",
"execution_count": 205,
"execution_count": 10,
"id": "97dd1b8c-724a-436a-88b1-b38204fc81f5",
"metadata": {},
"outputs": [],
@ -310,18 +312,22 @@
"\n",
"`What if we want to enforce tool use?`\n",
"\n",
"We can use fallbacks."
"We can use fallbacks.\n",
"\n",
"Let's select a code gen prompt that -- from some of my testing -- does not correctly invoke the tool.\n",
"\"Here's how you can write a retrieval-augmented generation (RAG) chain in LCEL:\""
"\"To build a RAG chain using LCEL, we'll use a vector store to retrieve relevant documents, a prompt template that incorporates the retrieved context, a chat model (like OpenAI) to generate a response based on the prompt, and an output parser to clean up the model output.\""
"'# Create a vector store retriever\\nvectorstore = DocArrayInMemorySearch.from_texts(\\n [\"harrison worked at kensho\", \"bears like to eat honey\"], \\n embedding=OpenAIEmbeddings(),\\n)\\nretriever = vectorstore.as_retriever()\\n\\n# Define a prompt template\\ntemplate = \"\"\"Answer the question based only on the following context:\\n{context}\\n\\nQuestion: {question}\"\"\"\\nprompt = ChatPromptTemplate.from_template(template)\\n\\n# Define the model and output parser \\nmodel = ChatOpenAI()\\noutput_parser = StrOutputParser()\\n\\n# Compose the chain using LCEL\\nsetup_and_retrieval = RunnableParallel(\\n {\"context\": retriever, \"question\": RunnablePassthrough()}\\n)\\nchain = setup_and_retrieval | prompt | model | output_parser\\n\\n# Test the chain\\nchain.invoke(\"where did harrison work?\")'"
"'vectorstore = DocArrayInMemorySearch.from_texts(\\n [\"harrison worked at kensho\", \"bears like to eat honey\"], \\n embedding=OpenAIEmbeddings(),\\n)\\n\\nretriever = vectorstore.as_retriever()\\n\\ntemplate = \"\"\"Answer the question based only on the following context:\\n{context}\\nQuestion: {question}\"\"\"\\nprompt = ChatPromptTemplate.from_template(template)\\n\\noutput_parser = StrOutputParser()\\n\\nrag_chain = (\\n {\"context\": retriever, \"question\": RunnablePassthrough()} \\n | prompt \\n | ChatOpenAI()\\n | output_parser\\n)\\n\\nprint(rag_chain.invoke(\"where did harrison work?\"))'"
]
},
"execution_count": 212,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
@ -502,19 +542,19 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e27cd768-a274-4ded-b7ba-14aaf6c57164",
"cell_type": "markdown",
"id": "80d63a3d-bad8-4385-bd85-40ca95c260c6",
"metadata": {},
"outputs": [],
"source": [
"exec(parsed_result_lcel.code)"
"Example trace catching an error and correcting:\n",