@langchain/openai 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,141 @@
1
+ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
+ import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
3
+ import { type BaseMessage } from "@langchain/core/messages";
4
+ import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
5
+ import { StructuredTool } from "@langchain/core/tools";
6
+ import { BaseChatModel, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
7
+ import type { BaseFunctionCallOptions } from "@langchain/core/language_models/base";
8
+ import type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
9
+ export type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput };
10
+ interface TokenUsage {
11
+ completionTokens?: number;
12
+ promptTokens?: number;
13
+ totalTokens?: number;
14
+ }
15
+ interface OpenAILLMOutput {
16
+ tokenUsage: TokenUsage;
17
+ }
18
+ type OpenAIRoleEnum = "system" | "assistant" | "user" | "function" | "tool";
19
+ export declare function messageToOpenAIRole(message: BaseMessage): OpenAIRoleEnum;
20
+ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCallOptions {
21
+ tools?: StructuredTool[] | OpenAIClient.ChatCompletionTool[];
22
+ tool_choice?: OpenAIClient.ChatCompletionToolChoiceOption;
23
+ promptIndex?: number;
24
+ response_format?: {
25
+ type: "json_object";
26
+ };
27
+ seed?: number;
28
+ }
29
+ /**
30
+ * Wrapper around OpenAI large language models that use the Chat endpoint.
31
+ *
32
+ * To use you should have the `openai` package installed, with the
33
+ * `OPENAI_API_KEY` environment variable set.
34
+ *
35
+ * To use with Azure you should have the `openai` package installed, with the
36
+ * `AZURE_OPENAI_API_KEY`,
37
+ * `AZURE_OPENAI_API_INSTANCE_NAME`,
38
+ * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
39
+ * and `AZURE_OPENAI_API_VERSION` environment variable set.
40
+ * `AZURE_OPENAI_BASE_PATH` is optional and will override `AZURE_OPENAI_API_INSTANCE_NAME` if you need to use a custom endpoint.
41
+ *
42
+ * @remarks
43
+ * Any parameters that are valid to be passed to {@link
44
+ * https://platform.openai.com/docs/api-reference/chat/create |
45
+ * `openai.createChatCompletion`} can be passed through {@link modelKwargs}, even
46
+ * if not explicitly available on this class.
47
+ * @example
48
+ * ```typescript
49
+ * // Create a new instance of ChatOpenAI with specific temperature and model name settings
50
+ * const model = new ChatOpenAI({
51
+ * temperature: 0.9,
52
+ * modelName: "ft:gpt-3.5-turbo-0613:{ORG_NAME}::{MODEL_ID}",
53
+ * });
54
+ *
55
+ * // Invoke the model with a message and await the response
56
+ * const message = await model.invoke("Hi there!");
57
+ *
58
+ * // Log the response to the console
59
+ * console.log(message);
60
+ *
61
+ * ```
62
+ */
63
+ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatModel<CallOptions> implements OpenAIChatInput, AzureOpenAIInput {
64
+ static lc_name(): string;
65
+ get callKeys(): string[];
66
+ lc_serializable: boolean;
67
+ get lc_secrets(): {
68
+ [key: string]: string;
69
+ } | undefined;
70
+ get lc_aliases(): Record<string, string>;
71
+ temperature: number;
72
+ topP: number;
73
+ frequencyPenalty: number;
74
+ presencePenalty: number;
75
+ n: number;
76
+ logitBias?: Record<string, number>;
77
+ modelName: string;
78
+ modelKwargs?: OpenAIChatInput["modelKwargs"];
79
+ stop?: string[];
80
+ user?: string;
81
+ timeout?: number;
82
+ streaming: boolean;
83
+ maxTokens?: number;
84
+ openAIApiKey?: string;
85
+ azureOpenAIApiVersion?: string;
86
+ azureOpenAIApiKey?: string;
87
+ azureOpenAIApiInstanceName?: string;
88
+ azureOpenAIApiDeploymentName?: string;
89
+ azureOpenAIBasePath?: string;
90
+ organization?: string;
91
+ private client;
92
+ private clientConfig;
93
+ constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & BaseChatModelParams & {
94
+ configuration?: ClientOptions & LegacyOpenAIInput;
95
+ },
96
+ /** @deprecated */
97
+ configuration?: ClientOptions & LegacyOpenAIInput);
98
+ /**
99
+ * Get the parameters used to invoke the model
100
+ */
101
+ invocationParams(options?: this["ParsedCallOptions"]): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
102
+ /** @ignore */
103
+ _identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
104
+ model_name: string;
105
+ } & ClientOptions;
106
+ _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
107
+ /**
108
+ * Get the identifying parameters for the model
109
+ *
110
+ */
111
+ identifyingParams(): Omit<OpenAIClient.Chat.Completions.ChatCompletionCreateParams, "messages"> & {
112
+ model_name: string;
113
+ } & ClientOptions;
114
+ /** @ignore */
115
+ _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
116
+ /**
117
+ * Estimate the number of tokens a prompt will use.
118
+ * Modified from: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts
119
+ */
120
+ private getEstimatedTokenCountFromPrompt;
121
+ /**
122
+ * Estimate the number of tokens an array of generations have used.
123
+ */
124
+ private getNumTokensFromGenerations;
125
+ getNumTokensFromMessages(messages: BaseMessage[]): Promise<{
126
+ totalCount: number;
127
+ countPerMessage: number[];
128
+ }>;
129
+ /**
130
+ * Calls the OpenAI API with retry logic in case of failures.
131
+ * @param request The request to send to the OpenAI API.
132
+ * @param options Optional configuration for the API call.
133
+ * @returns The response from the OpenAI API.
134
+ */
135
+ completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
136
+ completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
137
+ private _getClientOptions;
138
+ _llmType(): string;
139
+ /** @ignore */
140
+ _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
141
+ }