-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into brace/cohere-token-count
- Loading branch information
Showing
21 changed files
with
630 additions
and
7 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
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,25 @@ | ||
--- | ||
sidebar_label: Deep Infra | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# ChatDeepInfra | ||
|
||
LangChain supports chat models hosted by [Deep Infra](https://deepinfra.com/) through the `ChatDeepInfra` wrapper. | ||
First, you'll need to install the `@langchain/community` package: | ||
|
||
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; | ||
|
||
<IntegrationInstallTooltip></IntegrationInstallTooltip> | ||
|
||
```bash npm2yarn | ||
npm install @langchain/community | ||
``` | ||
|
||
You'll need to obtain an API key and set it as an environment variable named `DEEPINFRA_API_TOKEN` | ||
(or pass it into the constructor), then call the model as shown below: | ||
|
||
import Example from "@examples/models/chat/integration_deepinfra.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</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
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,25 @@ | ||
--- | ||
sidebar_label: Deep Infra | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# DeepInfra | ||
|
||
LangChain supports LLMs hosted by [Deep Infra](https://deepinfra.com/) through the `DeepInfra` wrapper. | ||
First, you'll need to install the `@langchain/community` package: | ||
|
||
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; | ||
|
||
<IntegrationInstallTooltip></IntegrationInstallTooltip> | ||
|
||
```bash npm2yarn | ||
npm install @langchain/community | ||
``` | ||
|
||
You'll need to obtain an API key and set it as an environment variable named `DEEPINFRA_API_TOKEN` | ||
(or pass it into the constructor), then call the model as shown below: | ||
|
||
import Example from "@examples/models/llm/deepinfra.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</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,17 @@ | ||
import { ChatDeepInfra } from "@langchain/community/chat_models/deepinfra"; | ||
import { HumanMessage } from "@langchain/core/messages"; | ||
|
||
const apiKey = process.env.DEEPINFRA_API_TOKEN; | ||
|
||
const model = "meta-llama/Meta-Llama-3-70B-Instruct"; | ||
|
||
const chat = new ChatDeepInfra({ | ||
model, | ||
apiKey, | ||
}); | ||
|
||
const messages = [new HumanMessage("Hello")]; | ||
|
||
const res = await chat.invoke(messages); | ||
|
||
console.log(res); |
85 changes: 85 additions & 0 deletions
85
examples/src/models/chat/integration_openai_parallel_tool_calls.ts
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,85 @@ | ||
import { ChatOpenAI } from "@langchain/openai"; | ||
import { z } from "zod"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
|
||
const model = new ChatOpenAI({ | ||
temperature: 0, | ||
model: "gpt-4o", | ||
}); | ||
|
||
// Define your tools | ||
const calculatorSchema = z | ||
.object({ | ||
operation: z.enum(["add", "subtract", "multiply", "divide"]), | ||
number1: z.number(), | ||
number2: z.number(), | ||
}) | ||
.describe("A tool to perform basic arithmetic operations"); | ||
const weatherSchema = z | ||
.object({ | ||
city: z.enum(["add", "subtract", "multiply", "divide"]), | ||
}) | ||
.describe("A tool to get the weather in a city"); | ||
|
||
// Bind tools to the model | ||
const modelWithTools = model.bindTools([ | ||
{ | ||
type: "function", | ||
function: { | ||
name: "calculator", | ||
description: calculatorSchema.description, | ||
parameters: zodToJsonSchema(calculatorSchema), | ||
}, | ||
}, | ||
{ | ||
type: "function", | ||
function: { | ||
name: "weather", | ||
description: weatherSchema.description, | ||
parameters: zodToJsonSchema(weatherSchema), | ||
}, | ||
}, | ||
]); | ||
|
||
// Invoke the model with `parallel_tool_calls` set to `true` | ||
const response = await modelWithTools.invoke( | ||
["What is the weather in san francisco and what is 23716 times 27342?"], | ||
{ | ||
parallel_tool_calls: true, | ||
} | ||
); | ||
console.log(response.tool_calls); | ||
// We can see it called two tools | ||
/* | ||
[ | ||
{ | ||
name: 'weather', | ||
args: { city: 'san francisco' }, | ||
id: 'call_c1KymEIix7mdlFtgLSnTXmDc' | ||
}, | ||
{ | ||
name: 'calculator', | ||
args: { operation: 'multiply', number1: 23716, number2: 27342 }, | ||
id: 'call_ANLYclAmXQ4TwUCLXakbPr3Z' | ||
} | ||
] | ||
*/ | ||
|
||
// Invoke the model with `parallel_tool_calls` set to `false` | ||
const response2 = await modelWithTools.invoke( | ||
["What is the weather in san francisco and what is 23716 times 27342?"], | ||
{ | ||
parallel_tool_calls: false, | ||
} | ||
); | ||
console.log(response2.tool_calls); | ||
// We can see it called one tool | ||
/* | ||
[ | ||
{ | ||
name: 'weather', | ||
args: { city: 'san francisco' }, | ||
id: 'call_Rk34XffawJjgZ2BCK9E4CwlT' | ||
} | ||
] | ||
*/ |
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,18 @@ | ||
import { DeepInfraLLM } from "@langchain/community/llms/deepinfra"; | ||
|
||
const apiKey = process.env.DEEPINFRA_API_TOKEN; | ||
const model = "meta-llama/Meta-Llama-3-70B-Instruct"; | ||
|
||
const llm = new DeepInfraLLM({ | ||
temperature: 0.7, | ||
maxTokens: 20, | ||
model, | ||
apiKey, | ||
maxRetries: 5, | ||
}); | ||
|
||
const res = await llm.invoke( | ||
"What is the next step in the process of making a good game?" | ||
); | ||
|
||
console.log({ res }); |
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
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
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
Oops, something went wrong.