Skip to content

Commit

Permalink
docs[patch]: Adds RAG guide to concepts, semantic similarity to howtos (
Browse files Browse the repository at this point in the history
#5783)

* Adds concepts guide

* Format, fix link
  • Loading branch information
jacoblee93 authored Jun 17, 2024
1 parent 340a6b9 commit 9822a5c
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ docs/how_to/assign.mdx
docs/how_to/agent_executor.md
docs/how_to/agent_executor.mdx
docs/integrations/llms/mistral.md
docs/integrations/llms/mistral.mdx
docs/integrations/llms/mistral.mdx
155 changes: 138 additions & 17 deletions docs/core_docs/docs/concepts.mdx

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion docs/core_docs/docs/how_to/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ We'll illustrate both methods using a two step sequence where the first step cla
You can use a custom function to route between different outputs. Here's an example:

import CodeBlock from "@theme/CodeBlock";
import BranchExample from "@examples/guides/expression_language/how_to_routing_runnable_branch.ts";

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

Expand All @@ -42,6 +41,14 @@ import FactoryFunctionExample from "@examples/guides/expression_language/how_to_

<CodeBlock language="typescript">{FactoryFunctionExample}</CodeBlock>

## Routing by semantic similarity

One especially useful technique is to use embeddings to route a query to the most relevant prompt. Here's an example:

import SemanticSimilarityExample from "@examples/guides/expression_language/how_to_routing_semantic_similarity.ts";

<CodeBlock language="typescript">{SemanticSimilarityExample}</CodeBlock>

## Using a RunnableBranch

A `RunnableBranch` is initialized with a list of (condition, runnable) pairs and a default runnable. It selects which branch by passing each condition the input it's invoked with. It selects the first condition to evaluate to True, and runs the corresponding runnable to that condition with the input.
Expand All @@ -50,6 +57,8 @@ If no provided conditions match, it runs the default runnable.

Here's an example of what it looks like in action:

import BranchExample from "@examples/guides/expression_language/how_to_routing_runnable_branch.ts";

<CodeBlock language="typescript">{BranchExample}</CodeBlock>

## Next steps
Expand Down
Binary file added docs/core_docs/static/img/langgraph_rag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/core_docs/static/img/rag_landscape.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ChatAnthropic } from "@langchain/anthropic";
import { OpenAIEmbeddings } from "@langchain/openai";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { cosineSimilarity } from "@langchain/core/utils/math";

const physicsTemplate = `You are a very smart physics professor.
You are great at answering questions about physics in a concise and easy to understand manner.
When you don't know the answer to a question you admit that you don't know.
Do not use more than 100 words.
Here is a question:
{query}`;

const mathTemplate = `"You are a very good mathematician. You are great at answering math questions.
You are so good because you are able to break down hard problems into their component parts,
answer the component parts, and then put them together to answer the broader question.
Do not use more than 100 words.
Here is a question:
{query}`;

const embeddings = new OpenAIEmbeddings({});

const templates = [physicsTemplate, mathTemplate];
const templateEmbeddings = await embeddings.embedDocuments(templates);

const promptRouter = async (query: string) => {
const queryEmbedding = await embeddings.embedQuery(query);
const similarity = cosineSimilarity([queryEmbedding], templateEmbeddings)[0];
const isPhysicsQuestion = similarity[0] > similarity[1];
let promptTemplate: ChatPromptTemplate;
if (isPhysicsQuestion) {
console.log(`Using physics prompt`);
promptTemplate = ChatPromptTemplate.fromTemplate(templates[0]);
} else {
console.log(`Using math prompt`);
promptTemplate = ChatPromptTemplate.fromTemplate(templates[1]);
}
return promptTemplate.invoke({ query });
};

const chain = RunnableSequence.from([
promptRouter,
new ChatAnthropic({ model: "claude-3-haiku-20240307" }),
new StringOutputParser(),
]);

console.log(await chain.invoke("what's a black hole?"));

/*
Using physics prompt
*/

/*
A black hole is a region in space where the gravitational pull is so strong that nothing, not even light, can escape from it. It is the result of the gravitational collapse of a massive star, creating a singularity surrounded by an event horizon, beyond which all information is lost. Black holes have fascinated scientists for decades, as they provide insights into the most extreme conditions in the universe and the nature of gravity itself. While we understand the basic properties of black holes, there are still many unanswered questions about their behavior and their role in the cosmos.
*/

console.log(await chain.invoke("what's a path integral?"));

/*
Using math prompt
*/

/*
A path integral is a mathematical formulation in quantum mechanics used to describe the behavior of a particle or system. It considers all possible paths the particle can take between two points, and assigns a probability amplitude to each path. By summing up the contributions from all paths, it provides a comprehensive understanding of the particle's quantum mechanical behavior. This approach allows for the calculation of complex quantum phenomena, such as quantum tunneling and interference effects, making it a powerful tool in theoretical physics.
*/

0 comments on commit 9822a5c

Please sign in to comment.