Skip to content

Commit

Permalink
docs: Tutorials up to date (#1734)
Browse files Browse the repository at this point in the history
* edits

* add js code to web voyager
  • Loading branch information
isahers1 authored Sep 17, 2024
1 parent 0bf4bf8 commit bfd18f7
Show file tree
Hide file tree
Showing 23 changed files with 1,398 additions and 587 deletions.

Large diffs are not rendered by default.

97 changes: 19 additions & 78 deletions docs/docs/tutorials/chatbots/information-gather-prompting.ipynb

Large diffs are not rendered by default.

141 changes: 101 additions & 40 deletions docs/docs/tutorials/code_assistant/langgraph_code_assistant.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,28 @@
"\n",
"### Code solution\n",
"\n",
"Try OpenAI and [Claude3](https://docs.anthropic.com/en/docs/about-claude/models) with function calling.\n",
"First, we will try OpenAI and [Claude3](https://docs.anthropic.com/en/docs/about-claude/models) with function calling.\n",
"\n",
"Create `code_gen_chain` w/ either OpenAI or Claude and test here."
"We will create a `code_gen_chain` w/ either OpenAI or Claude and test them here."
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 5,
"id": "3ba3df70-f6b4-4ea5-a210-e10944960bc6",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"code(prefix='To build a Retrieval-Augmented Generation (RAG) chain in LCEL, you will need to set up a chain that combines a retriever and a language model (LLM). The retriever will fetch relevant documents based on a query, and the LLM will generate a response using the retrieved documents as context. Here’s how you can do it:', imports='from langchain_core.prompts import ChatPromptTemplate\\nfrom langchain_openai import ChatOpenAI\\nfrom langchain_core.output_parsers import StrOutputParser\\nfrom langchain_core.retrievers import MyRetriever', code='# Define the retriever\\nretriever = MyRetriever() # Replace with your specific retriever implementation\\n\\n# Define the LLM model\\nmodel = ChatOpenAI(model=\"gpt-4\")\\n\\n# Create a prompt template for the LLM\\nprompt_template = ChatPromptTemplate.from_template(\"Given the following documents, answer the question: {question}\\nDocuments: {documents}\")\\n\\n# Create the RAG chain\\nrag_chain = prompt_template | retriever | model | StrOutputParser()\\n\\n# Example usage\\nquery = \"What are the benefits of using RAG?\"\\nresponse = rag_chain.invoke({\"question\": query})\\nprint(response)')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_openai import ChatOpenAI\n",
Expand All @@ -162,24 +173,24 @@
"\n",
"# Data model\n",
"class code(BaseModel):\n",
" \"\"\"Code output\"\"\"\n",
" \"\"\"Schema for code solutions to questions about LCEL.\"\"\"\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",
" description = \"Schema for code solutions to questions about LCEL.\"\n",
"\n",
"\n",
"expt_llm = \"gpt-4-0125-preview\"\n",
"expt_llm = \"gpt-4o-mini\"\n",
"llm = ChatOpenAI(temperature=0, model=expt_llm)\n",
"code_gen_chain = code_gen_prompt | llm.with_structured_output(code)\n",
"code_gen_chain_oai = code_gen_prompt | llm.with_structured_output(code)\n",
"question = \"How do I build a RAG chain in LCEL?\"\n",
"# solution = code_gen_chain_oai.invoke({\"context\":concatenated_content,\"messages\":[(\"user\",question)]})"
"solution = code_gen_chain_oai.invoke({\"context\":concatenated_content,\"messages\":[(\"user\",question)]})\n",
"solution"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"id": "cd30b67d-96db-4e51-a540-ae23fcc1f878",
"metadata": {},
"outputs": [],
Expand All @@ -205,18 +216,7 @@
")\n",
"\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",
" description = \"Schema for code solutions to questions about LCEL.\"\n",
"\n",
"\n",
"# LLM\n",
"# expt_llm = \"claude-3-haiku-20240307\"\n",
"expt_llm = \"claude-3-opus-20240229\"\n",
"llm = ChatAnthropic(\n",
" model=expt_llm,\n",
Expand Down Expand Up @@ -297,12 +297,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "9f14750f-dddc-485b-ba29-5392cdf4ba43",
"metadata": {
"scrolled": true
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"code(prefix=\"To build a RAG (Retrieval Augmented Generation) chain in LCEL, you can use a retriever to fetch relevant documents and then pass those documents to a chat model to generate a response based on the retrieved context. Here's an example of how to do this:\", imports='from langchain_expressions import retrieve, chat_completion', code='question = \"What is the capital of France?\"\\n\\nrelevant_docs = retrieve(question)\\n\\nresult = chat_completion(\\n model=\\'openai-gpt35\\', \\n messages=[\\n {{{\"role\": \"system\", \"content\": \"Answer the question based on the retrieved context.}}},\\n {{{\"role\": \"user\", \"content\": \\'\\'\\'\\n Context: {relevant_docs}\\n Question: {question}\\n \\'\\'\\'}}\\n ]\\n)\\n\\nprint(result)')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Test\n",
"question = \"How do I build a RAG chain in LCEL?\"\n",
Expand All @@ -324,7 +335,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"id": "c185f1a2-e943-4bed-b833-4243c9c64092",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -361,7 +372,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"id": "b70e8301-63ae-4f7e-ad8f-c9a052fe3566",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -537,7 +548,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 10,
"id": "f66b4e00-4731-42c8-bc38-72dd0ff7c92c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -569,13 +580,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "9bcaafe4-ddcf-4fab-8620-2d9b6c508f98",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---GENERATING CODE SOLUTION---\n",
"---CHECKING CODE---\n",
"---CODE IMPORT CHECK: FAILED---\n",
"---DECISION: RE-TRY SOLUTION---\n",
"---GENERATING CODE SOLUTION---\n",
"---CHECKING CODE---\n",
"---CODE IMPORT CHECK: FAILED---\n",
"---DECISION: RE-TRY SOLUTION---\n",
"---GENERATING CODE SOLUTION---\n",
"---CHECKING CODE---\n",
"---CODE BLOCK CHECK: FAILED---\n",
"---DECISION: FINISH---\n"
]
}
],
"source": [
"question = \"How can I directly pass a string to a runnable and use it to construct the input needed for my prompt?\"\n",
"app.invoke({\"messages\": [(\"user\", question)], \"iterations\": 0})"
"solution = app.invoke({\"messages\": [(\"user\", question)], \"iterations\": 0, \"error\":\"\"})"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "9d28692e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"code(prefix='To directly pass a string to a runnable and use it to construct the input needed for a prompt, you can use the `_from_value` method on a PromptTemplate in LCEL. Create a PromptTemplate with the desired template string, then call `_from_value` on it with a dictionary mapping the input variable names to their values. This will return a PromptValue that you can pass directly to any chain or model that accepts a prompt input.', imports='from langchain_core.prompts import PromptTemplate', code='user_string = \"langchain is awesome\"\\n\\nprompt_template = PromptTemplate.from_template(\"Tell me more about how {user_input}.\")\\n\\nprompt_value = prompt_template._from_value({\"user_input\": user_string})\\n\\n# Pass the PromptValue directly to a model or chain \\nchain.run(prompt_value)')"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solution['generation']"
]
},
{
Expand All @@ -593,14 +644,14 @@
"source": [
"[Here](https://smith.langchain.com/public/326674a6-62bd-462d-88ae-eea49d503f9d/d) is a public dataset of LCEL questions. \n",
"\n",
"I saved this as `test-LCEL-code-gen`.\n",
"I saved this as `lcel-teacher-eval`.\n",
"\n",
"You can also find the csv [here](https://github.com/langchain-ai/lcel-teacher/blob/main/eval/eval.csv)."
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 19,
"id": "678e8954-56b5-4cc6-be26-f7f2a060b242",
"metadata": {},
"outputs": [],
Expand All @@ -612,10 +663,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "ef7cf662-7a6f-4dee-965c-6309d4045feb",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"Dataset(name='lcel-teacher-eval', description='Eval set for LCEL teacher', data_type=<DataType.kv: 'kv'>, id=UUID('8b57696d-14ea-4f00-9997-b3fc74a16846'), created_at=datetime.datetime(2024, 9, 16, 22, 50, 4, 169288, tzinfo=datetime.timezone.utc), modified_at=datetime.datetime(2024, 9, 16, 22, 50, 4, 169288, tzinfo=datetime.timezone.utc), example_count=0, session_count=0, last_session_start_time=None, inputs_schema=None, outputs_schema=None)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Clone the dataset to your tenant to use it\n",
"public_dataset = (\n",
Expand All @@ -634,7 +696,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 21,
"id": "455a34ea-52cb-4ae5-9f4a-7e4a08cd0c09",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -671,7 +733,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 33,
"id": "c8fa6bcb-b245-4422-b79a-582cd8a7d7ea",
"metadata": {},
"outputs": [],
Expand All @@ -681,20 +743,19 @@
" solution = code_gen_chain.invoke(\n",
" {\"context\": concatenated_content, \"messages\": [(\"user\", example[\"question\"])]}\n",
" )\n",
" solution_structured = code_gen_chain.invoke([(\"code\", solution)])\n",
" return {\"imports\": solution_structured.imports, \"code\": solution_structured.code}\n",
" return {\"imports\": solution.imports, \"code\": solution.code}\n",
"\n",
"\n",
"def predict_langgraph(example: dict):\n",
" \"\"\"LangGraph\"\"\"\n",
" graph = app.invoke({\"messages\": [(\"user\", example[\"question\"])], \"iterations\": 0})\n",
" graph = app.invoke({\"messages\": [(\"user\", example[\"question\"])], \"iterations\": 0, \"error\": \"\"})\n",
" solution = graph[\"generation\"]\n",
" return {\"imports\": solution.imports, \"code\": solution.code}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 34,
"id": "d9c57468-97f6-47d6-a5e9-c09b53bfdd83",
"metadata": {},
"outputs": [],
Expand All @@ -705,7 +766,7 @@
"code_evalulator = [check_import, check_execution]\n",
"\n",
"# Dataset\n",
"dataset_name = \"test-LCEL-code-gen\""
"dataset_name = \"lcel-teacher-eval\""
]
},
{
Expand Down
Loading

0 comments on commit bfd18f7

Please sign in to comment.