[Docs] Add ruff linting to .ipynb files (#645)

This commit is contained in:
William FH
2024-06-11 18:02:49 -07:00
committed by GitHub
parent 854bf2c295
commit 4eab1739da
67 changed files with 741 additions and 850 deletions
@@ -94,9 +94,9 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"### OpenAI\n",
"\n",
@@ -125,6 +125,7 @@
" code: str = Field(description=\"Code block not including import statements\")\n",
" description = \"Schema for code solutions to questions about LCEL.\"\n",
"\n",
"\n",
"expt_llm = \"gpt-4-0125-preview\"\n",
"llm = ChatOpenAI(temperature=0, model=expt_llm)\n",
"code_gen_chain = code_gen_prompt | llm.with_structured_output(code)\n",
@@ -181,6 +182,7 @@
"\n",
"structured_llm_claude = llm.with_structured_output(code, include_raw=True)\n",
"\n",
"\n",
"# Optional: Check for errors in case tool use is flaky\n",
"def check_claude_output(tool_output):\n",
" \"\"\"Check for parse error or failure to call the tool\"\"\"\n",
@@ -189,7 +191,7 @@
" if tool_output[\"parsing_error\"]:\n",
" # Report back output and parsing errors\n",
" print(\"Parsing error!\")\n",
" raw_output = str(code_output[\"raw\"].content)\n",
" raw_output = str(tool_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",
@@ -199,15 +201,17 @@
" elif not tool_output[\"parsed\"]:\n",
" print(\"Failed to invoke tool!\")\n",
" raise ValueError(\n",
" f\"You did not use the provided tool! Be sure to invoke the tool to structure the output.\"\n",
" \"You did not use the provided tool! Be sure to invoke the tool to structure the output.\"\n",
" )\n",
" return tool_output\n",
"\n",
"\n",
"# Chain with output check\n",
"code_chain_claude_raw = (\n",
" code_gen_prompt_claude | structured_llm_claude | check_claude_output\n",
")\n",
"\n",
"\n",
"def insert_errors(inputs):\n",
" \"\"\"Insert errors for tool parsing in the messages\"\"\"\n",
"\n",
@@ -240,6 +244,7 @@
"\n",
" return solution[\"parsed\"]\n",
"\n",
"\n",
"# Optional: With re-try to correct for failure to invoke tool\n",
"code_gen_chain = code_gen_chain_re_try | parse_output\n",
"\n",
@@ -281,7 +286,7 @@
"metadata": {},
"outputs": [],
"source": [
"from typing import Dict, TypedDict, List\n",
"from typing import List, TypedDict\n",
"\n",
"\n",
"class GraphState(TypedDict):\n",
@@ -318,10 +323,7 @@
"metadata": {},
"outputs": [],
"source": [
"from operator import itemgetter\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"from langchain_core.runnables import RunnablePassthrough\n",
"from langchain_core.prompts import PromptTemplate\n",
"\n",
"### Parameter\n",
"\n",
@@ -396,7 +398,6 @@
" iterations = state[\"iterations\"]\n",
"\n",
" # Get solution components\n",
" prefix = code_solution.prefix\n",
" imports = code_solution.imports\n",
" code = code_solution.code\n",
"\n",
@@ -457,14 +458,6 @@
" code_solution = state[\"generation\"]\n",
"\n",
" # Prompt reflection\n",
" reflection_message = [\n",
" (\n",
" \"user\",\n",
" \"\"\"You tried to solve this problem and failed a unit test. Reflect on this failure\n",
" given the provided documentation. Write a few key suggestions based on the \n",
" documentation to avoid making this mistake again.\"\"\",\n",
" )\n",
" ]\n",
"\n",
" # Add reflection\n",
" reflections = code_gen_chain.invoke(\n",
@@ -613,7 +606,7 @@
" try:\n",
" exec(imports)\n",
" return {\"key\": \"import_check\", \"score\": 1}\n",
" except:\n",
" except Exception:\n",
" return {\"key\": \"import_check\", \"score\": 0}\n",
"\n",
"\n",
@@ -623,7 +616,7 @@
" try:\n",
" exec(imports + \"\\n\" + code)\n",
" return {\"key\": \"code_execution_check\", \"score\": 1}\n",
" except:\n",
" except Exception:\n",
" return {\"key\": \"code_execution_check\", \"score\": 0}"
]
},
@@ -754,7 +747,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.2"
}
},
"nbformat": 4,