UNPKG

@langchain/community

Version:
1 lines 17.8 kB
{"version":3,"file":"fireworks.cjs","names":["ChatOpenAICompletions"],"sources":["../../src/chat_models/fireworks.ts"],"sourcesContent":["import type {\n BaseChatModelParams,\n LangSmithParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport {\n type OpenAIClient,\n type ChatOpenAICallOptions,\n type OpenAIChatInput,\n type OpenAICoreRequestOptions,\n ChatOpenAICompletions,\n} from \"@langchain/openai\";\n\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\ntype FireworksUnsupportedArgs =\n | \"frequencyPenalty\"\n | \"presencePenalty\"\n | \"logitBias\"\n | \"functions\";\n\ntype FireworksUnsupportedCallOptions = \"functions\" | \"function_call\";\n\nexport type ChatFireworksCallOptions = Partial<\n Omit<ChatOpenAICallOptions, FireworksUnsupportedCallOptions>\n>;\n\n/**\n * Wrapper around Fireworks API for large language models fine-tuned for chat\n *\n * Fireworks API is compatible to the OpenAI API with some limitations described in\n * https://readme.fireworks.ai/docs/openai-compatibility.\n *\n * To use, you should have the `FIREWORKS_API_KEY` environment variable set.\n *\n * Setup:\n * Install `@langchain/community` and set a environment variable called `FIREWORKS_API_KEY`.\n *\n * ```bash\n * npm install @langchain/community\n * export FIREWORKS_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/langchain_community_chat_models_fireworks.ChatFireworks.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_openai.ChatOpenAICallOptions.html)\n *\n * Because the Fireworks API extends OpenAI's, the call option type is the same.\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * stop: [\"\\n\"],\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatFireworks } from '@langchain/community/chat_models/fireworks';\n *\n * const llm = new ChatFireworks({\n * model: \"command-r-plus\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"id\": \"dbc233df-532e-4aaa-8995-9d6ea65fea15\",\n * \"content\": \"The translation of \\\"I love programming\\\" into French is:\\n\\n\\\"J'adore la programmation.\\\"\\n\\nHere's a breakdown of the translation:\\n\\n* \\\"I\\\" is translated to \\\"Je\\\" (but in informal writing, it's common to use \\\"J'\\\" instead of \\\"Je\\\" when it's followed by a vowel)\\n* \\\"love\\\" is translated to \\\"adore\\\"\\n* \\\"programming\\\" is translated to \\\"la programmation\\\"\\n\\nSo, the complete translation is \\\"J'adore la programmation.\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"tokenUsage\": {\n * \"completionTokens\": 105,\n * \"promptTokens\": 19,\n * \"totalTokens\": 124\n * },\n * \"finish_reason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 19,\n * \"output_tokens\": 105,\n * \"total_tokens\": 124\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \"The translation\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \" of \\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \"I love\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \" programming\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \" into French\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \" is:\\n\\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \"\\\"J\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ...\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \"ation.\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"id\": \"ed5fc403-b7ed-4447-819f-f9645ea0277c\",\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 19,\n * \"output_tokens\": 105,\n * \"total_tokens\": 124\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"id\": \"9b80e5af-0f50-4fb7-b700-6d431a819556\",\n * \"content\": \"The translation of \\\"I love programming\\\" into French is:\\n\\n\\\"J'adore la programmation.\\\"\\n\\nHere's a breakdown of the translation:\\n\\n* \\\"I\\\" is translated to \\\"Je\\\" (but in informal writing, it's common to use \\\"J'\\\" instead of \\\"Je\\\" when it's followed by a vowel)\\n* \\\"love\\\" is translated to \\\"adore\\\"\\n* \\\"programming\\\" is translated to \\\"la programmation\\\"\\n\\nSo, the complete translation is \\\"J'adore la programmation.\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"prompt\": 0,\n * \"completion\": 0,\n * \"finish_reason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 19,\n * \"output_tokens\": 105,\n * \"total_tokens\": 124\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const llmForToolCalling = new ChatFireworks({\n * // Use a model with tool calling capability\n * model: \"accounts/fireworks/models/firefunction-v2\",\n * temperature: 0,\n * // other params...\n * });\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call',\n * id: 'call_9DE0WnhgKDbxu6HyHOkDQFub'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call',\n * id: 'call_58lcAPTqQyiqepxynwARhGs8'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call',\n * id: 'call_r0m6AFoqaMvPp4Zt5aEAc0oE'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call',\n * id: 'call_mENaPG1ryOF44BmaW4VkBaSi'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: 'Why did the cat join a band?',\n * punchline: 'Because it wanted to be the purr-cussionist!',\n * rating: 8\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n *\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 277, output_tokens: 8, total_tokens: 285 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Response Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForResponseMetadata = await llm.invoke(input);\n * console.log(aiMsgForResponseMetadata.response_metadata);\n * ```\n *\n * ```txt\n * {\n * tokenUsage: { completionTokens: 8, promptTokens: 277, totalTokens: 285 },\n * finish_reason: 'stop'\n * }\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatFireworks extends ChatOpenAICompletions<ChatFireworksCallOptions> {\n static lc_name() {\n return \"ChatFireworks\";\n }\n\n _llmType() {\n return \"fireworks\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n fireworksApiKey: \"FIREWORKS_API_KEY\",\n apiKey: \"FIREWORKS_API_KEY\",\n };\n }\n\n lc_serializable = true;\n\n fireworksApiKey?: string;\n\n apiKey?: string;\n\n constructor(\n fields?: Partial<\n Omit<OpenAIChatInput, \"openAIApiKey\" | FireworksUnsupportedArgs>\n > &\n BaseChatModelParams & {\n /**\n * Prefer `apiKey`\n */\n fireworksApiKey?: string;\n /**\n * The Fireworks API key to use.\n */\n apiKey?: string;\n }\n ) {\n const fireworksApiKey =\n fields?.apiKey ||\n fields?.fireworksApiKey ||\n getEnvironmentVariable(\"FIREWORKS_API_KEY\");\n\n if (!fireworksApiKey) {\n throw new Error(\n `Fireworks API key not found. Please set the FIREWORKS_API_KEY environment variable or provide the key into \"fireworksApiKey\"`\n );\n }\n\n super({\n ...fields,\n model:\n fields?.model ||\n fields?.modelName ||\n \"accounts/fireworks/models/llama-v3p1-8b-instruct\",\n apiKey: fireworksApiKey,\n configuration: {\n baseURL: \"https://api.fireworks.ai/inference/v1\",\n },\n streamUsage: false,\n });\n\n this.fireworksApiKey = fireworksApiKey;\n this.apiKey = fireworksApiKey;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n const params = super.getLsParams(options);\n params.ls_provider = \"fireworks\";\n return params;\n }\n\n toJSON() {\n const result = super.toJSON();\n\n if (\n \"kwargs\" in result &&\n typeof result.kwargs === \"object\" &&\n result.kwargs != null\n ) {\n delete result.kwargs.openai_api_key;\n delete result.kwargs.configuration;\n }\n\n return result;\n }\n\n async completionWithRetry(\n request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;\n\n async completionWithRetry(\n request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;\n\n /**\n * Calls the Fireworks API with retry logic in case of failures.\n * @param request The request to send to the Fireworks API.\n * @param options Optional configuration for the API call.\n * @returns The response from the Fireworks API.\n */\n async completionWithRetry(\n request:\n | OpenAIClient.Chat.ChatCompletionCreateParamsStreaming\n | OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<\n | AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>\n | OpenAIClient.Chat.Completions.ChatCompletion\n > {\n delete request.frequency_penalty;\n delete request.presence_penalty;\n delete request.logit_bias;\n delete request.functions;\n\n if (request.stream === true) {\n return super.completionWithRetry(request, options);\n }\n\n return super.completionWithRetry(request, options);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAicA,IAAa,gBAAb,cAAmCA,kBAAAA,sBAAgD;CACjF,OAAO,UAAU;AACf,SAAO;;CAGT,WAAW;AACT,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO;GACL,iBAAiB;GACjB,QAAQ;GACT;;CAGH,kBAAkB;CAElB;CAEA;CAEA,YACE,QAaA;EACA,MAAM,kBACJ,QAAQ,UACR,QAAQ,oBAAA,GAAA,0BAAA,wBACe,oBAAoB;AAE7C,MAAI,CAAC,gBACH,OAAM,IAAI,MACR,+HACD;AAGH,QAAM;GACJ,GAAG;GACH,OACE,QAAQ,SACR,QAAQ,aACR;GACF,QAAQ;GACR,eAAe,EACb,SAAS,yCACV;GACD,aAAa;GACd,CAAC;AAEF,OAAK,kBAAkB;AACvB,OAAK,SAAS;;CAGhB,YAAY,SAAqD;EAC/D,MAAM,SAAS,MAAM,YAAY,QAAQ;AACzC,SAAO,cAAc;AACrB,SAAO;;CAGT,SAAS;EACP,MAAM,SAAS,MAAM,QAAQ;AAE7B,MACE,YAAY,UACZ,OAAO,OAAO,WAAW,YACzB,OAAO,UAAU,MACjB;AACA,UAAO,OAAO,OAAO;AACrB,UAAO,OAAO,OAAO;;AAGvB,SAAO;;;;;;;;CAmBT,MAAM,oBACJ,SAGA,SAIA;AACA,SAAO,QAAQ;AACf,SAAO,QAAQ;AACf,SAAO,QAAQ;AACf,SAAO,QAAQ;AAEf,MAAI,QAAQ,WAAW,KACrB,QAAO,MAAM,oBAAoB,SAAS,QAAQ;AAGpD,SAAO,MAAM,oBAAoB,SAAS,QAAQ"}