forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
68 changed files
with
3,344 additions
and
1,196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,6 @@ Chinook_Sqlite.sql | |
*.swo | ||
|
||
.node-version | ||
|
||
firebase-debug.log | ||
firestore-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_label: Integrations | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# Integrations: Chat Models | ||
|
||
LangChain offers a number of Chat Models implementations that integrate with various model providers. These are: | ||
|
||
## `ChatOpenAI` | ||
|
||
import OpenAI from "@examples/models/chat/integration_openai.ts"; | ||
|
||
<CodeBlock language="typescript">{OpenAI}</CodeBlock> | ||
|
||
## Azure `ChatOpenAI` | ||
|
||
import AzureOpenAI from "@examples/models/chat/integration_azure_openai.ts"; | ||
|
||
<CodeBlock language="typescript">{AzureOpenAI}</CodeBlock> | ||
|
||
## `ChatAnthropic` | ||
|
||
import Anthropic from "@examples/models/chat/integration_anthropic.ts"; | ||
|
||
<CodeBlock language="typescript">{Anthropic}</CodeBlock> | ||
|
||
## `PromptLayerChatOpenAI` | ||
|
||
You can pass in the optional `returnPromptLayerId` boolean to get a `promptLayerRequestId` like below. Here is an example of getting the PromptLayerChatOpenAI requestID: | ||
|
||
```typescript | ||
import { PromptLayerChatOpenAI } from "langchain/chat_models/openai"; | ||
|
||
const chat = new PromptLayerChatOpenAI({ | ||
returnPromptLayerId: true, | ||
}); | ||
|
||
const respA = await chat.generate([ | ||
[ | ||
new SystemMessage( | ||
"You are a helpful assistant that translates English to French." | ||
), | ||
], | ||
]); | ||
|
||
console.log(JSON.stringify(respA, null, 3)); | ||
|
||
/* | ||
{ | ||
"generations": [ | ||
[ | ||
{ | ||
"text": "Bonjour! Je suis un assistant utile qui peut vous aider à traduire de l'anglais vers le français. Que puis-je faire pour vous aujourd'hui?", | ||
"message": { | ||
"type": "ai", | ||
"data": { | ||
"content": "Bonjour! Je suis un assistant utile qui peut vous aider à traduire de l'anglais vers le français. Que puis-je faire pour vous aujourd'hui?" | ||
} | ||
}, | ||
"generationInfo": { | ||
"promptLayerRequestId": 2300682 | ||
} | ||
} | ||
] | ||
], | ||
"llmOutput": { | ||
"tokenUsage": { | ||
"completionTokens": 35, | ||
"promptTokens": 19, | ||
"totalTokens": 54 | ||
} | ||
} | ||
} | ||
*/ | ||
``` | ||
|
||
## `ChatGooglePaLM` | ||
|
||
The [Google PaLM API](https://developers.generativeai.google/products/palm) can be integrated by first | ||
installing the required packages: | ||
|
||
```bash npm2yarn | ||
npm install google-auth-library @google-ai/generativelanguage | ||
``` | ||
|
||
Create an **API key** from [Google MakerSuite](https://makersuite.google.com/app/apikey). You can then set | ||
the key as `GOOGLE_PALM_API_KEY` environment variable or pass it as `apiKey` parameter while instantiating | ||
the model. | ||
|
||
import GooglePaLMExample from "@examples/models/chat/integration_googlepalm.ts"; | ||
|
||
<CodeBlock language="typescript">{GooglePaLMExample}</CodeBlock> | ||
|
||
## `Google Vertex AI` | ||
|
||
The Vertex AI implementation is meant to be used in Node.js and not | ||
directly from a browser, since it requires a service account to use. | ||
|
||
Before running this code, you should make sure the Vertex AI API is | ||
enabled for the relevant project and that you've authenticated to | ||
Google Cloud using one of these methods: | ||
|
||
- You are logged into an account (using `gcloud auth application-default login`) | ||
permitted to that project. | ||
- You are running on a machine using a service account that is permitted | ||
to the project. | ||
- You have downloaded the credentials for a service account that is permitted | ||
to the project and set the `GOOGLE_APPLICATION_CREDENTIALS` environment | ||
variable to the path of this file. | ||
|
||
```bash npm2yarn | ||
npm install google-auth-library | ||
``` | ||
|
||
The ChatGoogleVertexAI class works just like other chat-based LLMs, | ||
with a few exceptions: | ||
|
||
1. The first `SystemMessage` passed in is mapped to the "context" parameter that the PaLM model expects. | ||
No other `SystemMessages` are allowed. | ||
2. After the first `SystemMessage`, there must be an odd number of messages, representing a conversation between a human and the model. | ||
3. Human messages must alternate with AI messages. | ||
|
||
import ChatGoogleVertexAI from "@examples/models/chat/integration_googlevertexai.ts"; | ||
|
||
<CodeBlock language="typescript">{ChatGoogleVertexAI}</CodeBlock> | ||
|
||
There is also an optional `examples` constructor parameter that can help the model understand what an appropriate response | ||
looks like. | ||
|
||
import ChatGoogleVertexAIExamples from "@examples/models/chat/integration_googlevertexai-examples.ts"; | ||
|
||
<CodeBlock language="typescript">{ChatGoogleVertexAIExamples}</CodeBlock> | ||
|
||
## `ChatBaiduWenxin` | ||
|
||
import Wenxin from "@examples/models/chat/integration_baiduwenxin.ts"; | ||
|
||
<CodeBlock language="typescript">{Wenxin}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_label: Integrations | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# Integrations: Embeddings | ||
|
||
LangChain offers a number of Embeddings implementations that integrate with various model providers. These are: | ||
|
||
## `OpenAIEmbeddings` | ||
|
||
The `OpenAIEmbeddings` class uses the OpenAI API to generate embeddings for a given text. By default it strips new line characters from the text, as recommended by OpenAI, but you can disable this by passing `stripNewLines: false` to the constructor. | ||
|
||
```typescript | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
|
||
const embeddings = new OpenAIEmbeddings({ | ||
openAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.OPENAI_API_KEY | ||
}); | ||
``` | ||
|
||
## Azure `OpenAIEmbeddings` | ||
|
||
The `OpenAIEmbeddings` class uses the OpenAI API on Azure to generate embeddings for a given text. By default it strips new line characters from the text, as recommended by OpenAI, but you can disable this by passing `stripNewLines: false` to the constructor. | ||
|
||
```typescript | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
|
||
const embeddings = new OpenAIEmbeddings({ | ||
azureOpenAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY | ||
azureOpenAIApiInstanceName: "YOUR-INSTANCE-NAME", // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME | ||
azureOpenAIApiDeploymentName: "YOUR-DEPLOYMENT-NAME", // In Node.js defaults to process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME | ||
azureOpenAIApiVersion: "YOUR-API-VERSION", // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION | ||
azureOpenAIBasePath: "YOUR-AZURE-OPENAI-BASE-PATH", // In Node.js defaults to process.env.AZURE_OPENAI_BASE_PATH | ||
}); | ||
``` | ||
|
||
## `GooglePaLMEmbeddings` | ||
|
||
The [Google PaLM API](https://developers.generativeai.google/products/palm) can be integrated by first | ||
installing the required packages: | ||
|
||
```bash npm2yarn | ||
npm install google-auth-library @google-ai/generativelanguage | ||
``` | ||
|
||
Create an **API key** from [Google MakerSuite](https://makersuite.google.com/app/apikey). You can then set | ||
the key as `GOOGLE_PALM_API_KEY` environment variable or pass it as `apiKey` parameter while instantiating | ||
the model. | ||
|
||
import GooglePaLMExample from "@examples/models/embeddings/googlepalm.ts"; | ||
|
||
<CodeBlock language="typescript">{GooglePaLMExample}</CodeBlock> | ||
|
||
## `Google Vertex AI` | ||
|
||
The `GoogleVertexAIEmbeddings` class uses Google's Vertex AI PaLM models | ||
to generate embeddings for a given text. | ||
|
||
The Vertex AI implementation is meant to be used in Node.js and not | ||
directly in a browser, since it requires a service account to use. | ||
|
||
Before running this code, you should make sure the Vertex AI API is | ||
enabled for the relevant project in your Google Cloud dashboard and that you've authenticated to | ||
Google Cloud using one of these methods: | ||
|
||
- You are logged into an account (using `gcloud auth application-default login`) | ||
permitted to that project. | ||
- You are running on a machine using a service account that is permitted | ||
to the project. | ||
- You have downloaded the credentials for a service account that is permitted | ||
to the project and set the `GOOGLE_APPLICATION_CREDENTIALS` environment | ||
variable to the path of this file. | ||
|
||
```bash npm2yarn | ||
npm install google-auth-library | ||
``` | ||
|
||
import GoogleVertexAIExample from "@examples/models/embeddings/googlevertexai.ts"; | ||
|
||
<CodeBlock language="typescript">{GoogleVertexAIExample}</CodeBlock> | ||
|
||
**Note:** The default Google Vertex AI embeddings model, `textembedding-gecko`, has a different number of dimensions than OpenAI's `text-embedding-ada-002` model | ||
and may not be supported by all vector store providers. | ||
|
||
## `CohereEmbeddings` | ||
|
||
The `CohereEmbeddings` class uses the Cohere API to generate embeddings for a given text. | ||
|
||
```bash npm2yarn | ||
npm install cohere-ai | ||
``` | ||
|
||
```typescript | ||
import { CohereEmbeddings } from "langchain/embeddings/cohere"; | ||
|
||
const embeddings = new CohereEmbeddings({ | ||
apiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.COHERE_API_KEY | ||
}); | ||
``` | ||
|
||
## `TensorFlowEmbeddings` | ||
|
||
This Embeddings integration runs the embeddings entirely in your browser or Node.js environment, using [TensorFlow.js](https://www.tensorflow.org/js). This means that your data isn't sent to any third party, and you don't need to sign up for any API keys. However, it does require more memory and processing power than the other integrations. | ||
|
||
```bash npm2yarn | ||
npm install @tensorflow/tfjs-core @tensorflow/tfjs-converter @tensorflow-models/universal-sentence-encoder @tensorflow/tfjs-backend-cpu | ||
``` | ||
|
||
```typescript | ||
import "@tensorflow/tfjs-backend-cpu"; | ||
import { TensorFlowEmbeddings } from "langchain/embeddings/tensorflow"; | ||
|
||
const embeddings = new TensorFlowEmbeddings(); | ||
``` | ||
|
||
This example uses the CPU backend, which works in any JS environment. However, you can use any of the backends supported by TensorFlow.js, including GPU and WebAssembly, which will be a lot faster. For Node.js you can use the `@tensorflow/tfjs-node` package, and for the browser you can use the `@tensorflow/tfjs-backend-webgl` package. See the [TensorFlow.js documentation](https://www.tensorflow.org/js/guide/platform_environment) for more information. | ||
|
||
## `HuggingFaceInferenceEmbeddings` | ||
|
||
This Embeddings integration uses the HuggingFace Inference API to generate embeddings for a given text using by default the `sentence-transformers/distilbert-base-nli-mean-tokens` model. You can pass a different model name to the constructor to use a different model. | ||
|
||
```bash npm2yarn | ||
npm install @huggingface/inference@1 | ||
``` | ||
|
||
```typescript | ||
import { HuggingFaceInferenceEmbeddings } from "langchain/embeddings/hf"; | ||
|
||
const embeddings = new HuggingFaceInferenceEmbeddings({ | ||
apiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.HUGGINGFACEHUB_API_KEY | ||
}); | ||
``` |
Oops, something went wrong.