-
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.
Showing
16 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
docs/extras/modules/model_io/models/llms/integrations/yandex.mdx
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,21 @@ | ||
# YandexGPT | ||
|
||
LangChain.js supports calling [YandexGPT](https://cloud.yandex.com/en/services/yandexgpt) LLMs. | ||
|
||
## Setup | ||
|
||
First, you should [create service account](https://cloud.yandex.com/en/docs/iam/operations/sa/create) with the `ai.languageModels.user` role. | ||
|
||
Next, you have two authentication options: | ||
|
||
- [IAM token](https://cloud.yandex.com/en/docs/iam/operations/iam-token/create-for-sa). | ||
You can specify the token in a constructor parameter `iam_token` or in an environment variable `YC_IAM_TOKEN`. | ||
- [API key](https://cloud.yandex.com/en/docs/iam/operations/api-key/create) | ||
You can specify the key in a constructor parameter `api_key` or in an environment variable `YC_API_KEY`. | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import YandexGPTExample from "@examples/models/llm/yandex.ts"; | ||
|
||
<CodeBlock language="typescript">{YandexGPTExample}</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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { YandexGPT } from "langchain/llms/yandex"; | ||
|
||
const model = new YandexGPT(); | ||
|
||
const res = await model.call('Translate "I love programming" into French.'); | ||
|
||
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
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,123 @@ | ||
import { getEnvironmentVariable } from "../util/env.js"; | ||
import { LLM, BaseLLMParams } from "./base.js"; | ||
|
||
const apiUrl = "https://llm.api.cloud.yandex.net/llm/v1alpha/instruct"; | ||
|
||
export interface YandexGPTInputs extends BaseLLMParams { | ||
/** | ||
* What sampling temperature to use. | ||
* Should be a double number between 0 (inclusive) and 1 (inclusive). | ||
*/ | ||
temperature?: number; | ||
|
||
/** | ||
* Maximum limit on the total number of tokens | ||
* used for both the input prompt and the generated response. | ||
*/ | ||
maxTokens?: number; | ||
|
||
/** Model name to use. */ | ||
model?: string; | ||
|
||
/** | ||
* Yandex Cloud Api Key for service account | ||
* with the `ai.languageModels.user` role. | ||
*/ | ||
apiKey?: string; | ||
|
||
/** | ||
* Yandex Cloud IAM token for service account | ||
* with the `ai.languageModels.user` role. | ||
*/ | ||
iamToken?: string; | ||
} | ||
|
||
export class YandexGPT extends LLM implements YandexGPTInputs { | ||
static lc_name() { | ||
return "Yandex GPT"; | ||
} | ||
|
||
get lc_secrets(): { [key: string]: string } | undefined { | ||
return { | ||
apiKey: "YC_API_KEY", | ||
iamToken: "YC_IAM_TOKEN", | ||
}; | ||
} | ||
|
||
temperature = 0.6; | ||
|
||
maxTokens = 1700; | ||
|
||
model = "general"; | ||
|
||
apiKey?: string; | ||
|
||
iamToken?: string; | ||
|
||
constructor(fields?: YandexGPTInputs) { | ||
super(fields ?? {}); | ||
|
||
const apiKey = fields?.apiKey ?? getEnvironmentVariable("YC_API_KEY"); | ||
|
||
const iamToken = fields?.iamToken ?? getEnvironmentVariable("YC_IAM_TOKEN"); | ||
|
||
if (apiKey === undefined && iamToken === undefined) { | ||
throw new Error( | ||
"Please set the YC_API_KEY or YC_IAM_TOKEN environment variable or pass it to the constructor as the apiKey or iamToken field." | ||
); | ||
} | ||
|
||
this.apiKey = apiKey; | ||
this.iamToken = iamToken; | ||
this.maxTokens = fields?.maxTokens ?? this.maxTokens; | ||
this.temperature = fields?.temperature ?? this.temperature; | ||
this.model = fields?.model ?? this.model; | ||
} | ||
|
||
_llmType() { | ||
return "yandexgpt"; | ||
} | ||
|
||
/** @ignore */ | ||
async _call( | ||
prompt: string, | ||
options: this["ParsedCallOptions"] | ||
): Promise<string> { | ||
// Hit the `generate` endpoint on the `large` model | ||
return this.caller.callWithOptions({ signal: options.signal }, async () => { | ||
const headers = { "Content-Type": "application/json", Authorization: "" }; | ||
if (this.apiKey !== undefined) { | ||
headers.Authorization = `Api-Key ${this.apiKey}`; | ||
} else { | ||
headers.Authorization = `Bearer ${this.iamToken}`; | ||
} | ||
const bodyData = { | ||
model: this.model, | ||
generationOptions: { | ||
temperature: this.temperature, | ||
maxTokens: this.maxTokens, | ||
}, | ||
|
||
requestText: prompt, | ||
}; | ||
|
||
try { | ||
const response = await fetch(apiUrl, { | ||
method: "POST", | ||
headers, | ||
body: JSON.stringify(bodyData), | ||
}); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to fetch ${apiUrl} from YandexGPT: ${response.status}` | ||
); | ||
} | ||
|
||
const responseData = await response.json(); | ||
return responseData.result.alternatives[0].text; | ||
} catch (error) { | ||
throw new Error(`Failed to fetch ${apiUrl} from YandexGPT ${error}`); | ||
} | ||
}); | ||
} | ||
} |
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
b74b340
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-docs – ./
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
js.langchain.com