From 7869b42e6e412078ea4a15c5edd965ad5282ebb2 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Mon, 11 Dec 2023 17:54:34 -0800 Subject: [PATCH 01/17] cr --- .../src/llms/tests/togetherai.int.test.ts | 37 +++ .../src/llms/togetherai.ts | 290 ++++++++++++++++++ 2 files changed, 327 insertions(+) create mode 100644 libs/langchain-community/src/llms/tests/togetherai.int.test.ts create mode 100644 libs/langchain-community/src/llms/togetherai.ts diff --git a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts new file mode 100644 index 000000000000..b651199f4d04 --- /dev/null +++ b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts @@ -0,0 +1,37 @@ +import { TogetherAI } from "../togetherai.js"; +import { ChatPromptTemplate } from "@langchain/core/prompts"; + +test("TogetherAI can make a request to an LLM", async () => { + const model = new TogetherAI({ + modelName: "togethercomputer/StripedHyena-Nous-7B" + }); + const prompt = ChatPromptTemplate.fromMessages([ + ["ai", "You are a helpful assistant."], + ["human", "Tell me a joke about bears."] + ]); + const chain = prompt.pipe(model); + const result = await chain.invoke({}); + console.log("result", result); +}); + +test.only("TogetherAI can stream responses", async () => { + const model = new TogetherAI({ + modelName: "togethercomputer/StripedHyena-Nous-7B", + streaming: true + }); + const prompt = ChatPromptTemplate.fromMessages([ + ["ai", "You are a helpful assistant."], + ["human", "Tell me a joke about bears."] + ]); + const chain = prompt.pipe(model); + const result = await chain.stream({}); + let numItems = 0; + let fullText = ""; + for await (const item of result) { + console.log("stream item", item); + fullText += item; + numItems += 1; + } + console.log(fullText); + expect(numItems).toBeGreaterThan(1); +}); diff --git a/libs/langchain-community/src/llms/togetherai.ts b/libs/langchain-community/src/llms/togetherai.ts new file mode 100644 index 000000000000..ac7fba219350 --- /dev/null +++ b/libs/langchain-community/src/llms/togetherai.ts @@ -0,0 +1,290 @@ +import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager"; +import { + LLM, + type BaseLLMCallOptions, + type BaseLLMParams +} from "@langchain/core/language_models/llms"; +import { GenerationChunk } from "@langchain/core/outputs"; +import { getEnvironmentVariable } from "@langchain/core/utils/env"; + +interface TogetherAIInferenceResult { + object: string; + status: string; + prompt: Array; + model: string; + model_owner: string; + tags: object; + num_returns: number; + args: { + model: string; + prompt: string; + temperature: number; + top_p: number; + top_k: number; + max_tokens: number; + }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + subjobs: Array; + output: { + choices: Array<{ + finish_reason: string; + index: number; + text: string; + }>; + raw_compute_time: number; + result_type: string; + }; +} + +/** + * Note that the modelPath is the only required parameter. For testing you + * can set this in the environment variable `LLAMA_PATH`. + */ +export interface TogetherAIInputs extends BaseLLMParams { + /** + * The API key to use for the TogetherAI API. + * @default {process.env.TOGETHER_AI_API_KEY} + */ + apiKey?: string; + /** + * The name of the model to query. + */ + modelName: string; + /** + * A decimal number that determines the degree of randomness in the response. + * A value of 1 will always yield the same output. + * A temperature less than 1 favors more correctness and is appropriate for question answering or summarization. + * A value greater than 1 introduces more randomness in the output. + * @default {0.7} + */ + temperature?: number; + /** + * Whether or not to stream tokens as they are generated. + * @default {false} + */ + streaming?: boolean; + /** + * The `topP` (nucleus) parameter is used to dynamically adjust the number of choices for each predicted token based on the cumulative probabilities. + * It specifies a probability threshold, below which all less likely tokens are filtered out. + * This technique helps to maintain diversity and generate more fluent and natural-sounding text. + * @default {0.7} + */ + topP?: number; + /** + * The `topK` parameter is used to limit the number of choices for the next predicted word or token. + * It specifies the maximum number of tokens to consider at each step, based on their probability of occurrence. + * This technique helps to speed up the generation process and can improve the quality of the generated text by focusing on the most likely options. + * @default {50} + */ + topK?: number; + /** + * A number that controls the diversity of generated text by reducing the likelihood of repeated sequences. + * Higher values decrease repetition. + * @default {1} + */ + repetitionPenalty?: number; + /** + * An integer that specifies how many top token log probabilities are included in the response for each token generation step. + */ + logprobs?: number; + /** + * Run an LLM-based input-output safeguard model on top of any model. + */ + safetyModel?: string; +} + +export interface TogetherAICallOptions + extends BaseLLMCallOptions, + Pick< + TogetherAIInputs, + | "modelName" + | "temperature" + | "topP" + | "topK" + | "repetitionPenalty" + | "logprobs" + | "safetyModel" + > {} + +export class TogetherAI extends LLM { + lc_serializable = true; + + declare CallOptions: TogetherAICallOptions; + + static inputs: TogetherAIInputs; + + temperature = 0.7; + + topP = 0.7; + + topK = 50; + + modelName: string; + + streaming = false; + + repetitionPenalty = 1; + + logprobs?: number; + + safetyModel?: string; + + private apiKey: string; + + private inferenceUrl = "https://api.together.xyz/inference"; + + static lc_name() { + return "TogetherAI"; + } + + constructor(inputs: TogetherAIInputs) { + super(inputs); + const apiKey = + inputs.apiKey ?? getEnvironmentVariable("TOGETHER_AI_API_KEY"); + if (!apiKey) { + throw new Error("TOGETHER_AI_API_KEY not found."); + } + this.apiKey = apiKey; + this.temperature = inputs?.temperature ?? this.temperature; + this.topK = inputs?.topK ?? this.topK; + this.topP = inputs?.topP ?? this.topP; + this.modelName = inputs.modelName; + this.streaming = inputs.streaming ?? this.streaming; + this.repetitionPenalty = inputs.repetitionPenalty ?? this.repetitionPenalty; + this.logprobs = inputs.logprobs; + this.safetyModel = inputs.safetyModel; + } + + _llmType() { + return "together_ai"; + } + + private constructHeaders() { + return { + accept: "application/json", + "content-type": "application/json", + Authorization: `Bearer ${this.apiKey}` + }; + } + + private constructBody(prompt: string, options?: this["ParsedCallOptions"]) { + const body = { + model: options?.modelName ?? this?.modelName, + prompt, + temperature: this?.temperature ?? options?.temperature, + top_k: this?.topK ?? options?.topK, + top_p: this?.topP ?? options?.topP, + repetition_penalty: this?.repetitionPenalty ?? options?.repetitionPenalty, + logprobs: this?.logprobs ?? options?.logprobs, + stream_tokens: this?.streaming, + safety_model: this?.safetyModel ?? options?.safetyModel + }; + return body; + } + + async completionWithRetry( + prompt: string, + options?: this["ParsedCallOptions"] + ) { + return this.caller.call(async () => { + const fetchResponse = await fetch(this.inferenceUrl, { + method: "POST", + headers: { + ...this.constructHeaders() + }, + body: JSON.stringify(this.constructBody(prompt, options)) + }); + if (fetchResponse.status === 200) { + return fetchResponse.json(); + } + const errorResponse = await fetchResponse.json(); + throw new Error( + `Error getting prompt completion from Together AI. ${JSON.stringify( + errorResponse, + null, + 2 + )}` + ); + }); + } + + /** @ignore */ + async _call( + prompt: string, + options?: this["ParsedCallOptions"] + ): Promise { + try { + const response: TogetherAIInferenceResult = + await this.completionWithRetry(prompt, options); + const outputText = response.output.choices[0].text; + return outputText ?? ""; + } catch (e) { + // `completionWithRetry` will throw an error with the error response from Together AI. + // If it does throw, we want to re-throw verbatim. + throw e; + } + } + + async *_streamResponseChunks( + prompt: string, + options: this["ParsedCallOptions"], + runManager?: CallbackManagerForLLMRun + ): AsyncGenerator { + const fetchResponse = await fetch(this.inferenceUrl, { + method: "POST", + headers: { + ...this.constructHeaders() + }, + body: JSON.stringify(this.constructBody(prompt, options)) + }); + + if (fetchResponse.status !== 200) { + const errorResponse = await fetchResponse.json(); + throw new Error( + `Error getting prompt completion from Together AI. ${JSON.stringify( + errorResponse, + null, + 2 + )}` + ); + } + const reader = fetchResponse.body?.getReader(); + if (!reader) { + throw new Error("No reader found on fetch response."); + } + + try { + while (true) { + const { done, value } = await reader.read(); + if (done) { + break; + } + + const stringifiedResponse = `{${new TextDecoder().decode( + value + )}}`.replace("data:", `"data":`); + + if (!stringifiedResponse) { + continue; + } + + // Hacky way of checking if the response is a valid JSON object. + // If it is not, we can assume the stream is done. + if (!stringifiedResponse.includes(`"choices":[{"text":"`)) { + break; + } + + const parsedResponse = JSON.parse(stringifiedResponse); + yield new GenerationChunk({ + text: parsedResponse.data.choices[0].text, + generationInfo: {} + }); + await runManager?.handleLLMNewToken( + parsedResponse.data.choices[0].text + ); + } + } finally { + reader.releaseLock(); + } + } +} From d2bbe512bca0a886545cd372336b892a40616d88 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 10:34:26 -0800 Subject: [PATCH 02/17] lint --- .../src/llms/tests/togetherai.int.test.ts | 2 +- libs/langchain-community/src/llms/togetherai.ts | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts index b651199f4d04..3b237a2b97db 100644 --- a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts +++ b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts @@ -1,5 +1,5 @@ -import { TogetherAI } from "../togetherai.js"; import { ChatPromptTemplate } from "@langchain/core/prompts"; +import { TogetherAI } from "../togetherai.js"; test("TogetherAI can make a request to an LLM", async () => { const model = new TogetherAI({ diff --git a/libs/langchain-community/src/llms/togetherai.ts b/libs/langchain-community/src/llms/togetherai.ts index ac7fba219350..8b2a7d951a1d 100644 --- a/libs/langchain-community/src/llms/togetherai.ts +++ b/libs/langchain-community/src/llms/togetherai.ts @@ -213,16 +213,12 @@ export class TogetherAI extends LLM { prompt: string, options?: this["ParsedCallOptions"] ): Promise { - try { - const response: TogetherAIInferenceResult = - await this.completionWithRetry(prompt, options); - const outputText = response.output.choices[0].text; - return outputText ?? ""; - } catch (e) { - // `completionWithRetry` will throw an error with the error response from Together AI. - // If it does throw, we want to re-throw verbatim. - throw e; - } + const response: TogetherAIInferenceResult = await this.completionWithRetry( + prompt, + options + ); + const outputText = response.output.choices[0].text; + return outputText ?? ""; } async *_streamResponseChunks( From 0194cee6319993fb8a7152096502eb7ec5a4adfa Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 12:18:34 -0800 Subject: [PATCH 03/17] added docs & created entrypoint --- .../docs/integrations/llms/togetherai.mdx | 20 +++++++++++++++++++ examples/package.json | 1 + examples/src/models/llm/togetherai.ts | 19 ++++++++++++++++++ examples/src/models/llm/togetherai_stream.ts | 19 ++++++++++++++++++ libs/langchain-community/.gitignore | 3 +++ libs/langchain-community/package.json | 8 ++++++++ .../scripts/create-entrypoints.js | 1 + .../src/llms/togetherai.ts | 2 +- .../src/load/import_map.ts | 1 + yarn.lock | 3 ++- 10 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 docs/core_docs/docs/integrations/llms/togetherai.mdx create mode 100644 examples/src/models/llm/togetherai.ts create mode 100644 examples/src/models/llm/togetherai_stream.ts diff --git a/docs/core_docs/docs/integrations/llms/togetherai.mdx b/docs/core_docs/docs/integrations/llms/togetherai.mdx new file mode 100644 index 000000000000..bdd3edb0858b --- /dev/null +++ b/docs/core_docs/docs/integrations/llms/togetherai.mdx @@ -0,0 +1,20 @@ +import CodeBlock from "@theme/CodeBlock"; + +# Together AI + +Here's an example of calling a Together AI model as an LLM: + +import TogetherAI from "@examples/models/llm/togetherai.ts"; +import TogetherAIStream from "@examples/models/llm/togetherai_stream.ts"; + +{TogetherAI} + +You can run other models through Together by changing the `modelName` parameter. + +You can find a full list of models on [Together's website](https://api.together.xyz/playground). + +### Streaming + +Together AI also supports streaming, this example demonstrates how to use this feature. + +{TogetherAIStream} diff --git a/examples/package.json b/examples/package.json index bbc292a7a930..477068885925 100644 --- a/examples/package.json +++ b/examples/package.json @@ -27,6 +27,7 @@ "@getmetal/metal-sdk": "^4.0.0", "@getzep/zep-js": "^0.9.0", "@gomomento/sdk": "^1.51.1", + "@langchain/community": "workspace:*", "@opensearch-project/opensearch": "^2.2.0", "@pinecone-database/pinecone": "^1.1.0", "@planetscale/database": "^1.8.0", diff --git a/examples/src/models/llm/togetherai.ts b/examples/src/models/llm/togetherai.ts new file mode 100644 index 000000000000..7a59cce48008 --- /dev/null +++ b/examples/src/models/llm/togetherai.ts @@ -0,0 +1,19 @@ +import { TogetherAI } from "@langchain/community/llms/togetherai"; +import { PromptTemplate } from "langchain/prompts"; + +const model = new TogetherAI({ + modelName: "togethercomputer/StripedHyena-Nous-7B" +}); +const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. +User: {input}. +Assistant:`); +const chain = prompt.pipe(model); +const response = await chain.invoke({ + input: `Tell me a joke about bears` +}); +console.log("response", response); +/** +response Why don't bears use computers? + ### Response: +Because they find the mouse too difficult + */ diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts new file mode 100644 index 000000000000..31d155518a9d --- /dev/null +++ b/examples/src/models/llm/togetherai_stream.ts @@ -0,0 +1,19 @@ +import { TogetherAI } from "@langchain/community/llms/togetherai"; +import { PromptTemplate } from "langchain/prompts"; + +const model = new TogetherAI({ + modelName: "togethercomputer/StripedHyena-Nous-7B" +}); +const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. +User: {input}. +Assistant:`); +const chain = prompt.pipe(model); +const response = await chain.stream({ + input: "What's the capital of France?" +}); +for await (const item of response) { + console.log(item); +} +/** + * + */ diff --git a/libs/langchain-community/.gitignore b/libs/langchain-community/.gitignore index 9475e00e7a15..9413fc5248e4 100644 --- a/libs/langchain-community/.gitignore +++ b/libs/langchain-community/.gitignore @@ -163,6 +163,9 @@ llms/replicate.d.ts llms/sagemaker_endpoint.cjs llms/sagemaker_endpoint.js llms/sagemaker_endpoint.d.ts +llms/togetherai.cjs +llms/togetherai.js +llms/togetherai.d.ts llms/watsonx_ai.cjs llms/watsonx_ai.js llms/watsonx_ai.d.ts diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index a64b062a8129..3174fd296d38 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -740,6 +740,11 @@ "import": "./llms/sagemaker_endpoint.js", "require": "./llms/sagemaker_endpoint.cjs" }, + "./llms/togetherai": { + "types": "./llms/togetherai.d.ts", + "import": "./llms/togetherai.js", + "require": "./llms/togetherai.cjs" + }, "./llms/watsonx_ai": { "types": "./llms/watsonx_ai.d.ts", "import": "./llms/watsonx_ai.js", @@ -1379,6 +1384,9 @@ "llms/sagemaker_endpoint.cjs", "llms/sagemaker_endpoint.js", "llms/sagemaker_endpoint.d.ts", + "llms/togetherai.cjs", + "llms/togetherai.js", + "llms/togetherai.d.ts", "llms/watsonx_ai.cjs", "llms/watsonx_ai.js", "llms/watsonx_ai.d.ts", diff --git a/libs/langchain-community/scripts/create-entrypoints.js b/libs/langchain-community/scripts/create-entrypoints.js index 0a75fbb878eb..c129cc74b1d9 100644 --- a/libs/langchain-community/scripts/create-entrypoints.js +++ b/libs/langchain-community/scripts/create-entrypoints.js @@ -67,6 +67,7 @@ const entrypoints = { "llms/raycast": "llms/raycast", "llms/replicate": "llms/replicate", "llms/sagemaker_endpoint": "llms/sagemaker_endpoint", + "llms/togetherai": "llms/togetherai", "llms/watsonx_ai": "llms/watsonx_ai", "llms/writer": "llms/writer", "llms/yandex": "llms/yandex", diff --git a/libs/langchain-community/src/llms/togetherai.ts b/libs/langchain-community/src/llms/togetherai.ts index 8b2a7d951a1d..aadc524794d4 100644 --- a/libs/langchain-community/src/llms/togetherai.ts +++ b/libs/langchain-community/src/llms/togetherai.ts @@ -255,7 +255,7 @@ export class TogetherAI extends LLM { if (done) { break; } - + // The response is a stringified JSON object, except for the first key 'data' which is not stringified. const stringifiedResponse = `{${new TextDecoder().decode( value )}}`.replace("data:", `"data":`); diff --git a/libs/langchain-community/src/load/import_map.ts b/libs/langchain-community/src/load/import_map.ts index 89dacea4d86e..b42311bc9498 100644 --- a/libs/langchain-community/src/load/import_map.ts +++ b/libs/langchain-community/src/load/import_map.ts @@ -27,6 +27,7 @@ export * as llms__aleph_alpha from "../llms/aleph_alpha.js"; export * as llms__cloudflare_workersai from "../llms/cloudflare_workersai.js"; export * as llms__fireworks from "../llms/fireworks.js"; export * as llms__ollama from "../llms/ollama.js"; +export * as llms__togetherai from "../llms/togetherai.js"; export * as llms__yandex from "../llms/yandex.js"; export * as vectorstores__prisma from "../vectorstores/prisma.js"; export * as vectorstores__vectara from "../vectorstores/vectara.js"; diff --git a/yarn.lock b/yarn.lock index fda25b4bedc8..de400cc59e15 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7997,7 +7997,7 @@ __metadata: languageName: unknown linkType: soft -"@langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.2": +"@langchain/community@workspace:*, @langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.2": version: 0.0.0-use.local resolution: "@langchain/community@workspace:libs/langchain-community" dependencies: @@ -18442,6 +18442,7 @@ __metadata: "@getmetal/metal-sdk": ^4.0.0 "@getzep/zep-js": ^0.9.0 "@gomomento/sdk": ^1.51.1 + "@langchain/community": "workspace:*" "@opensearch-project/opensearch": ^2.2.0 "@pinecone-database/pinecone": ^1.1.0 "@planetscale/database": ^1.8.0 From 3f3e6c0fdd81c2c7f0598a79d28f0d8188d45c38 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 12:19:37 -0800 Subject: [PATCH 04/17] chore: lint files --- examples/src/models/llm/togetherai.ts | 4 ++-- examples/src/models/llm/togetherai_stream.ts | 4 ++-- .../src/llms/tests/togetherai.int.test.ts | 8 ++++---- libs/langchain-community/src/llms/togetherai.ts | 16 ++++++++-------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/src/models/llm/togetherai.ts b/examples/src/models/llm/togetherai.ts index 7a59cce48008..c1216b3ff5f8 100644 --- a/examples/src/models/llm/togetherai.ts +++ b/examples/src/models/llm/togetherai.ts @@ -2,14 +2,14 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B" + modelName: "togethercomputer/StripedHyena-Nous-7B", }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.invoke({ - input: `Tell me a joke about bears` + input: `Tell me a joke about bears`, }); console.log("response", response); /** diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index 31d155518a9d..5d6f6886d232 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -2,14 +2,14 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B" + modelName: "togethercomputer/StripedHyena-Nous-7B", }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.stream({ - input: "What's the capital of France?" + input: "What's the capital of France?", }); for await (const item of response) { console.log(item); diff --git a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts index 3b237a2b97db..0975e81f629d 100644 --- a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts +++ b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts @@ -3,11 +3,11 @@ import { TogetherAI } from "../togetherai.js"; test("TogetherAI can make a request to an LLM", async () => { const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B" + modelName: "togethercomputer/StripedHyena-Nous-7B", }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], - ["human", "Tell me a joke about bears."] + ["human", "Tell me a joke about bears."], ]); const chain = prompt.pipe(model); const result = await chain.invoke({}); @@ -17,11 +17,11 @@ test("TogetherAI can make a request to an LLM", async () => { test.only("TogetherAI can stream responses", async () => { const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", - streaming: true + streaming: true, }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], - ["human", "Tell me a joke about bears."] + ["human", "Tell me a joke about bears."], ]); const chain = prompt.pipe(model); const result = await chain.stream({}); diff --git a/libs/langchain-community/src/llms/togetherai.ts b/libs/langchain-community/src/llms/togetherai.ts index aadc524794d4..0a038d819205 100644 --- a/libs/langchain-community/src/llms/togetherai.ts +++ b/libs/langchain-community/src/llms/togetherai.ts @@ -2,7 +2,7 @@ import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager"; import { LLM, type BaseLLMCallOptions, - type BaseLLMParams + type BaseLLMParams, } from "@langchain/core/language_models/llms"; import { GenerationChunk } from "@langchain/core/outputs"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; @@ -163,7 +163,7 @@ export class TogetherAI extends LLM { return { accept: "application/json", "content-type": "application/json", - Authorization: `Bearer ${this.apiKey}` + Authorization: `Bearer ${this.apiKey}`, }; } @@ -177,7 +177,7 @@ export class TogetherAI extends LLM { repetition_penalty: this?.repetitionPenalty ?? options?.repetitionPenalty, logprobs: this?.logprobs ?? options?.logprobs, stream_tokens: this?.streaming, - safety_model: this?.safetyModel ?? options?.safetyModel + safety_model: this?.safetyModel ?? options?.safetyModel, }; return body; } @@ -190,9 +190,9 @@ export class TogetherAI extends LLM { const fetchResponse = await fetch(this.inferenceUrl, { method: "POST", headers: { - ...this.constructHeaders() + ...this.constructHeaders(), }, - body: JSON.stringify(this.constructBody(prompt, options)) + body: JSON.stringify(this.constructBody(prompt, options)), }); if (fetchResponse.status === 200) { return fetchResponse.json(); @@ -229,9 +229,9 @@ export class TogetherAI extends LLM { const fetchResponse = await fetch(this.inferenceUrl, { method: "POST", headers: { - ...this.constructHeaders() + ...this.constructHeaders(), }, - body: JSON.stringify(this.constructBody(prompt, options)) + body: JSON.stringify(this.constructBody(prompt, options)), }); if (fetchResponse.status !== 200) { @@ -273,7 +273,7 @@ export class TogetherAI extends LLM { const parsedResponse = JSON.parse(stringifiedResponse); yield new GenerationChunk({ text: parsedResponse.data.choices[0].text, - generationInfo: {} + generationInfo: {}, }); await runManager?.handleLLMNewToken( parsedResponse.data.choices[0].text From 079530f7f073bdc05d5dea552df456be99a65fc7 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Mon, 11 Dec 2023 18:20:11 -0800 Subject: [PATCH 05/17] all[patch]: Ensure other subpackages are built before test/build (#3624) * Ensure other subpackages are built before test/build * Fix test * Fix API ref build * Fix docs build * Fix build * Fix build * Build serially * Fix build --- .github/workflows/test-exports.yml | 14 +++++++------- docs/api_refs/package.json | 3 ++- docs/core_docs/package.json | 2 +- langchain-core/package.json | 3 +-- langchain-core/turbo.json | 23 ----------------------- langchain/package.json | 11 ++++++----- libs/langchain-anthropic/package.json | 9 +++++---- libs/langchain-community/package.json | 9 +++++---- libs/langchain-openai/package.json | 9 +++++---- package.json | 2 +- turbo.json | 17 +++++++++++++---- 11 files changed, 46 insertions(+), 56 deletions(-) delete mode 100644 langchain-core/turbo.json diff --git a/.github/workflows/test-exports.yml b/.github/workflows/test-exports.yml index 15b15338107b..bc5f0d1b16da 100644 --- a/.github/workflows/test-exports.yml +++ b/.github/workflows/test-exports.yml @@ -34,7 +34,7 @@ jobs: - name: Install dependencies run: yarn install --immutable - name: Build - run: yarn run build:deps && yarn workspace langchain build + run: yarn workspace langchain build shell: bash env: SKIP_API_DOCS: true @@ -54,7 +54,7 @@ jobs: - name: Install dependencies run: yarn install --immutable - name: Build - run: yarn run build:deps && yarn workspace langchain build + run: yarn workspace langchain build shell: bash env: SKIP_API_DOCS: true @@ -74,7 +74,7 @@ jobs: - name: Install dependencies run: yarn install --immutable - name: Build - run: yarn run build:deps && yarn workspace langchain build + run: yarn workspace langchain build shell: bash env: SKIP_API_DOCS: true @@ -94,7 +94,7 @@ jobs: - name: Install dependencies run: yarn install --immutable - name: Build - run: yarn run build:deps && yarn workspace langchain build + run: yarn workspace langchain build shell: bash env: SKIP_API_DOCS: true @@ -114,7 +114,7 @@ jobs: - name: Install dependencies run: yarn install --immutable - name: Build - run: yarn run build:deps && yarn workspace langchain build + run: yarn workspace langchain build shell: bash env: SKIP_API_DOCS: true @@ -134,7 +134,7 @@ jobs: - name: Install dependencies run: yarn install --immutable - name: Build - run: yarn run build:deps && yarn workspace langchain build + run: yarn workspace langchain build shell: bash env: SKIP_API_DOCS: true @@ -154,7 +154,7 @@ jobs: # - name: Install dependencies # run: yarn install --immutable # - name: Build - # run: yarn run build:deps && yarn workspace langchain build + # run: yarn workspace langchain build # shell: bash # env: # SKIP_API_DOCS: true diff --git a/docs/api_refs/package.json b/docs/api_refs/package.json index 3008943d4996..b87b7c3a010f 100644 --- a/docs/api_refs/package.json +++ b/docs/api_refs/package.json @@ -6,7 +6,8 @@ "dev": "next dev -p 3001", "typedoc": "npx typedoc --options typedoc.json", "build:scripts": "node ./scripts/generate-api-refs.js && node ./scripts/update-typedoc-css.js", - "build": "yarn run build:deps && yarn workspace langchain build && yarn build:scripts && next build", + "build": "yarn run build:deps && yarn build:scripts && next build", + "build:deps": "yarn run turbo:command build --filter=@langchain/core --filter=@langchain/anthropic --filter=@langchain/openai --filter=@langchain/community --filter=langchain --concurrency=1", "start": "yarn build && next start -p 3001", "lint": "next lint" }, diff --git a/docs/core_docs/package.json b/docs/core_docs/package.json index bd7067f7ba8e..7483f76e2cf1 100644 --- a/docs/core_docs/package.json +++ b/docs/core_docs/package.json @@ -6,7 +6,7 @@ "docusaurus": "docusaurus", "start": "yarn build:typedoc && rimraf ./docs/api && NODE_OPTIONS=--max-old-space-size=7168 docusaurus start", "build": "yarn build:typedoc && rimraf ./build && NODE_OPTIONS=--max-old-space-size=7168 DOCUSAURUS_SSR_CONCURRENCY=4 docusaurus build", - "build:typedoc": "cd ../api_refs && yarn build", + "build:typedoc": "yarn run turbo:command build --filter=api_refs", "swizzle": "docusaurus swizzle", "deploy": "docusaurus deploy", "clear": "docusaurus clear", diff --git a/langchain-core/package.json b/langchain-core/package.json index 0da434d283a8..581fd3e27fad 100644 --- a/langchain-core/package.json +++ b/langchain-core/package.json @@ -13,8 +13,7 @@ "url": "git@github.com:langchain-ai/langchainjs.git" }, "scripts": { - "build": "yarn turbo run build:scripts", - "build:envs": "yarn build:esm && yarn build:cjs", + "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn run build:scripts", "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests", "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs", "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch", diff --git a/langchain-core/turbo.json b/langchain-core/turbo.json deleted file mode 100644 index 5fe0abbb4ad8..000000000000 --- a/langchain-core/turbo.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "$schema": "https://turbo.build/schema.json", - "extends": ["//"], - "pipeline": { - "build:scripts": { - "outputs": [ - "tsconfig.json", - "package.json", - "**/*.js", - "**/*.d.ts", - "**/*.cjs", - "dist-cjs/**" - ], - "dependsOn": ["build:envs"] - }, - "build:envs": { - "dependsOn": ["clean"] - }, - "clean": { - "outputs": [".turbo/**", "dist/**"] - } - } -} diff --git a/langchain/package.json b/langchain/package.json index 76f56a380383..261853f7fcf1 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -859,7 +859,8 @@ "url": "git@github.com:langchain-ai/langchainjs.git" }, "scripts": { - "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build:deps": "yarn run turbo:command build --filter=@langchain/core --filter=@langchain/anthropic --filter=@langchain/openai --filter=@langchain/community --concurrency=1", "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests", "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs", "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch", @@ -872,10 +873,10 @@ "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre", "prepack": "yarn build", "release": "release-it --only-version --config .release-it.json", - "test": "cd ../ && yarn build:deps && cd ./langchain/ && yarn build && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", - "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", - "test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", - "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", + "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", + "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", + "test:integration": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", + "test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", "format": "prettier --write \"src\"", "format:check": "prettier --check \"src\"" }, diff --git a/libs/langchain-anthropic/package.json b/libs/langchain-anthropic/package.json index bc302635f1a0..9d52a75f4eaa 100644 --- a/libs/langchain-anthropic/package.json +++ b/libs/langchain-anthropic/package.json @@ -13,7 +13,8 @@ "url": "git@github.com:langchain-ai/langchainjs.git" }, "scripts": { - "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build:deps": "yarn run turbo:command build --filter=@langchain/core", "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/", "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs", "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch", @@ -25,9 +26,9 @@ "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre", "prepack": "yarn build", "release": "release-it --only-version --config .release-it.json", - "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", - "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", - "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", + "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", + "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", + "test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", "format": "prettier --write \"src\"", "format:check": "prettier --check \"src\"" }, diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index 3174fd296d38..7ac162768486 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -13,7 +13,8 @@ "url": "git@github.com:langchain-ai/langchainjs.git" }, "scripts": { - "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build:deps": "yarn run turbo:command build --filter=@langchain/core --filter=@langchain/anthropic --filter=@langchain/openai --concurrency=1", "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rm -rf dist/tests dist/**/tests", "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rm -rf dist-cjs", "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch", @@ -23,9 +24,9 @@ "clean": "rm -rf dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre", "prepack": "yarn build", "release": "release-it --only-version --config .release-it.json", - "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", - "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", - "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", + "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", + "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", + "test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", "test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%", "format": "prettier --write \"src\"", "format:check": "prettier --check \"src\"" diff --git a/libs/langchain-openai/package.json b/libs/langchain-openai/package.json index 5646a56f22ed..83cb8d10f798 100644 --- a/libs/langchain-openai/package.json +++ b/libs/langchain-openai/package.json @@ -13,7 +13,8 @@ "url": "git@github.com:langchain-ai/langchainjs.git" }, "scripts": { - "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts", + "build:deps": "yarn run turbo:command build --filter=@langchain/core", "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/", "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs", "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch", @@ -25,9 +26,9 @@ "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre", "prepack": "yarn build", "release": "release-it --only-version --config .release-it.json", - "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", - "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", - "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", + "test": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%", + "test:watch": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts", + "test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", "format": "prettier --write \"src\"", "format:check": "prettier --check \"src\"" }, diff --git a/package.json b/package.json index f8dce25ba422..f31d35986ea9 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "packageManager": "yarn@3.4.1", "scripts": { "build": "turbo run build --filter=\"!test-exports-*\" --concurrency 1", - "build:deps": "yarn workspace @langchain/core build && yarn workspace @langchain/anthropic build && yarn workspace @langchain/openai build && yarn workspace @langchain/community build", + "turbo:command": "turbo", "format": "turbo run format", "format:check": "turbo run format:check", "lint": "turbo run lint --concurrency 1", diff --git a/turbo.json b/turbo.json index 18bd38254a0f..f69769f2a5c3 100644 --- a/turbo.json +++ b/turbo.json @@ -2,15 +2,24 @@ "$schema": "https://turbo.build/schema.json", "globalDependencies": ["**/.env"], "pipeline": { - "@langchain/core#build": {}, + "@langchain/core#build": { + "outputs": ["dist/**", "dist-cjs/**", "*.js", "*.cjs", "*.d.ts"], + "inputs": ["src/**", "scripts/**", "package.json", "tsconfig.json"] + }, "@langchain/anthropic#build": { - "dependsOn": ["@langchain/core#build"] + "dependsOn": ["@langchain/core#build"], + "outputs": ["dist/**", "dist-cjs/**", "*.js", "*.cjs", "*.d.ts"], + "inputs": ["src/**", "scripts/**", "package.json", "tsconfig.json"] }, "@langchain/openai#build": { - "dependsOn": ["@langchain/core#build"] + "dependsOn": ["@langchain/core#build"], + "outputs": ["dist/**", "dist-cjs/**", "*.js", "*.cjs", "*.d.ts"], + "inputs": ["src/**", "scripts/**", "package.json", "tsconfig.json"] }, "@langchain/community#build": { - "dependsOn": ["@langchain/openai#build"] + "dependsOn": ["@langchain/openai#build"], + "outputs": ["dist/**", "dist-cjs/**", "*.js", "*.cjs", "*.d.ts"], + "inputs": ["src/**", "scripts/**", "package.json", "tsconfig.json"] }, "build": { "dependsOn": ["@langchain/core#build", "@langchain/community#build", "^build"], From a873b42d6b06bc8e375e8dfa61cddb03f352d1e8 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 12 Dec 2023 10:13:37 -0800 Subject: [PATCH 06/17] Bump core --- langchain-core/README.md | 2 +- langchain-core/package.json | 2 +- langchain/package.json | 6 +++--- .../template/package.json | 2 +- libs/langchain-anthropic/package.json | 5 ++--- libs/langchain-community/package.json | 6 +++--- libs/langchain-openai/package.json | 4 ++-- yarn.lock | 20 +++++++++---------- 8 files changed, 23 insertions(+), 24 deletions(-) diff --git a/langchain-core/README.md b/langchain-core/README.md index c69646b77823..508171fb6594 100644 --- a/langchain-core/README.md +++ b/langchain-core/README.md @@ -85,7 +85,7 @@ Because all used packages must share the same version of core, we suggest using "license": "MIT", "dependencies": { "@anthropic-ai/sdk": "^0.10.0", - "@langchain/core": "~0.0.1" + "@langchain/core": "~0.1.0" } } ``` diff --git a/langchain-core/package.json b/langchain-core/package.json index 581fd3e27fad..9a2a35e736b3 100644 --- a/langchain-core/package.json +++ b/langchain-core/package.json @@ -1,6 +1,6 @@ { "name": "@langchain/core", - "version": "0.0.11", + "version": "0.1.0", "description": "Core LangChain.js abstractions and schemas", "type": "module", "engines": { diff --git a/langchain/package.json b/langchain/package.json index 261853f7fcf1..f1b5ae16ca33 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -1175,9 +1175,9 @@ }, "dependencies": { "@anthropic-ai/sdk": "^0.9.1", - "@langchain/community": "~0.0.2", - "@langchain/core": "~0.0.11", - "@langchain/openai": "~0.0.2", + "@langchain/community": "~0.0.3", + "@langchain/core": "~0.1.0", + "@langchain/openai": "~0.0.5", "binary-extensions": "^2.2.0", "expr-eval": "^2.0.2", "js-tiktoken": "^1.0.7", diff --git a/libs/create-langchain-integration/template/package.json b/libs/create-langchain-integration/template/package.json index be75fdabefeb..3f05e51030c5 100644 --- a/libs/create-langchain-integration/template/package.json +++ b/libs/create-langchain-integration/template/package.json @@ -33,7 +33,7 @@ "author": "LangChain", "license": "MIT", "dependencies": { - "@langchain/core": "~0.0.6" + "@langchain/core": "~0.1.0" }, "devDependencies": { "@jest/globals": "^29.5.0", diff --git a/libs/langchain-anthropic/package.json b/libs/langchain-anthropic/package.json index 9d52a75f4eaa..bc5f86b00e6d 100644 --- a/libs/langchain-anthropic/package.json +++ b/libs/langchain-anthropic/package.json @@ -1,6 +1,6 @@ { "name": "@langchain/anthropic", - "version": "0.0.3", + "version": "0.0.4", "description": "Anthropic integrations for LangChain.js", "type": "module", "engines": { @@ -36,11 +36,10 @@ "license": "MIT", "dependencies": { "@anthropic-ai/sdk": "^0.10.0", - "@langchain/core": "~0.0.1" + "@langchain/core": "~0.1.0" }, "devDependencies": { "@jest/globals": "^29.5.0", - "@langchain/core": "workspace:*", "@swc/core": "^1.3.90", "@swc/jest": "^0.2.29", "dpdm": "^3.12.0", diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index 7ac162768486..fc75a3a9fa48 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -1,6 +1,6 @@ { "name": "@langchain/community", - "version": "0.0.2", + "version": "0.0.3", "description": "Sample integration for LangChain.js", "type": "module", "engines": { @@ -34,8 +34,8 @@ "author": "LangChain", "license": "MIT", "dependencies": { - "@langchain/core": "~0.0.11", - "@langchain/openai": "~0.0.2", + "@langchain/core": "~0.1.0", + "@langchain/openai": "~0.0.5", "flat": "^5.0.2", "langsmith": "~0.0.48", "uuid": "^9.0.0", diff --git a/libs/langchain-openai/package.json b/libs/langchain-openai/package.json index 83cb8d10f798..58144602af6f 100644 --- a/libs/langchain-openai/package.json +++ b/libs/langchain-openai/package.json @@ -1,6 +1,6 @@ { "name": "@langchain/openai", - "version": "0.0.2", + "version": "0.0.5", "description": "OpenAI integrations for LangChain.js", "type": "module", "engines": { @@ -35,7 +35,7 @@ "author": "LangChain", "license": "MIT", "dependencies": { - "@langchain/core": "~0.0.11", + "@langchain/core": "~0.1.0", "js-tiktoken": "^1.0.7", "openai": "^4.19.0", "zod": "^3.22.3", diff --git a/yarn.lock b/yarn.lock index de400cc59e15..d3b1f87a41ec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7977,7 +7977,7 @@ __metadata: dependencies: "@anthropic-ai/sdk": ^0.10.0 "@jest/globals": ^29.5.0 - "@langchain/core": "workspace:*" + "@langchain/core": ~0.1.0 "@swc/core": ^1.3.90 "@swc/jest": ^0.2.29 dpdm: ^3.12.0 @@ -7997,7 +7997,7 @@ __metadata: languageName: unknown linkType: soft -"@langchain/community@workspace:*, @langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.2": +"@langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.3": version: 0.0.0-use.local resolution: "@langchain/community@workspace:libs/langchain-community" dependencies: @@ -8023,8 +8023,8 @@ __metadata: "@gradientai/nodejs-sdk": ^1.2.0 "@huggingface/inference": ^2.6.4 "@jest/globals": ^29.5.0 - "@langchain/core": ~0.0.11 - "@langchain/openai": ~0.0.2 + "@langchain/core": ~0.1.0 + "@langchain/openai": ~0.0.5 "@mozilla/readability": ^0.4.4 "@opensearch-project/opensearch": ^2.2.0 "@pinecone-database/pinecone": ^1.1.0 @@ -8351,7 +8351,7 @@ __metadata: languageName: unknown linkType: soft -"@langchain/core@workspace:*, @langchain/core@workspace:langchain-core, @langchain/core@~0.0.11": +"@langchain/core@workspace:langchain-core, @langchain/core@~0.1.0": version: 0.0.0-use.local resolution: "@langchain/core@workspace:langchain-core" dependencies: @@ -8387,12 +8387,12 @@ __metadata: languageName: unknown linkType: soft -"@langchain/openai@workspace:libs/langchain-openai, @langchain/openai@~0.0.2": +"@langchain/openai@workspace:libs/langchain-openai, @langchain/openai@~0.0.5": version: 0.0.0-use.local resolution: "@langchain/openai@workspace:libs/langchain-openai" dependencies: "@jest/globals": ^29.5.0 - "@langchain/core": ~0.0.11 + "@langchain/core": ~0.1.0 "@swc/core": ^1.3.90 "@swc/jest": ^0.2.29 dpdm: ^3.12.0 @@ -22995,9 +22995,9 @@ __metadata: "@google-ai/generativelanguage": ^0.2.1 "@google-cloud/storage": ^6.10.1 "@jest/globals": ^29.5.0 - "@langchain/community": ~0.0.2 - "@langchain/core": ~0.0.11 - "@langchain/openai": ~0.0.2 + "@langchain/community": ~0.0.3 + "@langchain/core": ~0.1.0 + "@langchain/openai": ~0.0.5 "@notionhq/client": ^2.2.10 "@pinecone-database/pinecone": ^1.1.0 "@supabase/supabase-js": ^2.10.0 From 24ccd7cee11df1d3008658a7c898cadb1a9510e0 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 12 Dec 2023 10:27:13 -0800 Subject: [PATCH 07/17] Release 0.0.205 --- langchain/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain/package.json b/langchain/package.json index f1b5ae16ca33..461c08488333 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -1,6 +1,6 @@ { "name": "langchain", - "version": "0.0.204", + "version": "0.0.205", "description": "Typescript bindings for langchain", "type": "module", "engines": { From 850028158b20bdd231da425f960c0a372efbf551 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 12:18:34 -0800 Subject: [PATCH 08/17] added docs & created entrypoint --- examples/src/models/llm/togetherai.ts | 8 ++++++++ examples/src/models/llm/togetherai_stream.ts | 8 ++++++++ yarn.lock | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/src/models/llm/togetherai.ts b/examples/src/models/llm/togetherai.ts index c1216b3ff5f8..8c8018424012 100644 --- a/examples/src/models/llm/togetherai.ts +++ b/examples/src/models/llm/togetherai.ts @@ -2,14 +2,22 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ +<<<<<<< HEAD modelName: "togethercomputer/StripedHyena-Nous-7B", +======= + modelName: "togethercomputer/StripedHyena-Nous-7B" +>>>>>>> 076acc1c (added docs & created entrypoint) }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.invoke({ +<<<<<<< HEAD input: `Tell me a joke about bears`, +======= + input: `Tell me a joke about bears` +>>>>>>> 076acc1c (added docs & created entrypoint) }); console.log("response", response); /** diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index 5d6f6886d232..445d9061ff2a 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -2,14 +2,22 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ +<<<<<<< HEAD modelName: "togethercomputer/StripedHyena-Nous-7B", +======= + modelName: "togethercomputer/StripedHyena-Nous-7B" +>>>>>>> 076acc1c (added docs & created entrypoint) }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.stream({ +<<<<<<< HEAD input: "What's the capital of France?", +======= + input: "What's the capital of France?" +>>>>>>> 076acc1c (added docs & created entrypoint) }); for await (const item of response) { console.log(item); diff --git a/yarn.lock b/yarn.lock index d3b1f87a41ec..de2f810fe839 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7997,7 +7997,7 @@ __metadata: languageName: unknown linkType: soft -"@langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.3": +"@langchain/community@workspace:*, @langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.2": version: 0.0.0-use.local resolution: "@langchain/community@workspace:libs/langchain-community" dependencies: From ce378a3999e7f9d8061430436c0fd4897558270d Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 12:36:45 -0800 Subject: [PATCH 09/17] cr --- yarn.lock | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/yarn.lock b/yarn.lock index 8cfabe1e1fde..a1a9efa2f528 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7978,7 +7978,6 @@ __metadata: "@anthropic-ai/sdk": ^0.10.0 "@jest/globals": ^29.5.0 "@langchain/core": ~0.1.0 - "@langchain/core": ~0.1.0 "@swc/core": ^1.3.90 "@swc/jest": ^0.2.29 dpdm: ^3.12.0 @@ -7998,7 +7997,7 @@ __metadata: languageName: unknown linkType: soft -"@langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.3": +"@langchain/community@workspace:*, @langchain/community@workspace:libs/langchain-community, @langchain/community@~0.0.3": version: 0.0.0-use.local resolution: "@langchain/community@workspace:libs/langchain-community" dependencies: From ff126c95f7a0f8c2787007ada64f253c439e0e15 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 13:52:33 -0800 Subject: [PATCH 10/17] fixed stream --- .../src/llms/togetherai.ts | 63 ++++++------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/libs/langchain-community/src/llms/togetherai.ts b/libs/langchain-community/src/llms/togetherai.ts index 0a038d819205..8e5f2ace6621 100644 --- a/libs/langchain-community/src/llms/togetherai.ts +++ b/libs/langchain-community/src/llms/togetherai.ts @@ -2,10 +2,11 @@ import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager"; import { LLM, type BaseLLMCallOptions, - type BaseLLMParams, + type BaseLLMParams } from "@langchain/core/language_models/llms"; import { GenerationChunk } from "@langchain/core/outputs"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; +import { convertEventStreamToIterableReadableDataStream } from "../utils/event_source_parse.js"; interface TogetherAIInferenceResult { object: string; @@ -163,7 +164,7 @@ export class TogetherAI extends LLM { return { accept: "application/json", "content-type": "application/json", - Authorization: `Bearer ${this.apiKey}`, + Authorization: `Bearer ${this.apiKey}` }; } @@ -177,7 +178,7 @@ export class TogetherAI extends LLM { repetition_penalty: this?.repetitionPenalty ?? options?.repetitionPenalty, logprobs: this?.logprobs ?? options?.logprobs, stream_tokens: this?.streaming, - safety_model: this?.safetyModel ?? options?.safetyModel, + safety_model: this?.safetyModel ?? options?.safetyModel }; return body; } @@ -190,9 +191,9 @@ export class TogetherAI extends LLM { const fetchResponse = await fetch(this.inferenceUrl, { method: "POST", headers: { - ...this.constructHeaders(), + ...this.constructHeaders() }, - body: JSON.stringify(this.constructBody(prompt, options)), + body: JSON.stringify(this.constructBody(prompt, options)) }); if (fetchResponse.status === 200) { return fetchResponse.json(); @@ -229,12 +230,12 @@ export class TogetherAI extends LLM { const fetchResponse = await fetch(this.inferenceUrl, { method: "POST", headers: { - ...this.constructHeaders(), + ...this.constructHeaders() }, - body: JSON.stringify(this.constructBody(prompt, options)), + body: JSON.stringify(this.constructBody(prompt, options)) }); - if (fetchResponse.status !== 200) { + if (fetchResponse.status !== 200 ?? !fetchResponse.body) { const errorResponse = await fetchResponse.json(); throw new Error( `Error getting prompt completion from Together AI. ${JSON.stringify( @@ -244,43 +245,19 @@ export class TogetherAI extends LLM { )}` ); } - const reader = fetchResponse.body?.getReader(); - if (!reader) { - throw new Error("No reader found on fetch response."); - } - - try { - while (true) { - const { done, value } = await reader.read(); - if (done) { - break; - } - // The response is a stringified JSON object, except for the first key 'data' which is not stringified. - const stringifiedResponse = `{${new TextDecoder().decode( - value - )}}`.replace("data:", `"data":`); - - if (!stringifiedResponse) { - continue; - } - - // Hacky way of checking if the response is a valid JSON object. - // If it is not, we can assume the stream is done. - if (!stringifiedResponse.includes(`"choices":[{"text":"`)) { - break; - } - - const parsedResponse = JSON.parse(stringifiedResponse); - yield new GenerationChunk({ - text: parsedResponse.data.choices[0].text, - generationInfo: {}, + const stream = convertEventStreamToIterableReadableDataStream( + fetchResponse.body + ); + for await (const chunk of stream) { + if (chunk !== "[DONE]") { + const parsedChunk = JSON.parse(chunk); + const generationChunk = new GenerationChunk({ + text: parsedChunk.choices[0].text ?? "" }); - await runManager?.handleLLMNewToken( - parsedResponse.data.choices[0].text - ); + yield generationChunk; + // eslint-disable-next-line no-void + void runManager?.handleLLMNewToken(generationChunk.text ?? ""); } - } finally { - reader.releaseLock(); } } } From 9448c2737d21e2144576b8cad57fc868e54b92cc Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 14:28:04 -0800 Subject: [PATCH 11/17] cr --- examples/src/models/llm/togetherai.ts | 8 -------- examples/src/models/llm/togetherai_stream.ts | 8 -------- 2 files changed, 16 deletions(-) diff --git a/examples/src/models/llm/togetherai.ts b/examples/src/models/llm/togetherai.ts index 8c8018424012..7a59cce48008 100644 --- a/examples/src/models/llm/togetherai.ts +++ b/examples/src/models/llm/togetherai.ts @@ -2,22 +2,14 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ -<<<<<<< HEAD - modelName: "togethercomputer/StripedHyena-Nous-7B", -======= modelName: "togethercomputer/StripedHyena-Nous-7B" ->>>>>>> 076acc1c (added docs & created entrypoint) }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.invoke({ -<<<<<<< HEAD - input: `Tell me a joke about bears`, -======= input: `Tell me a joke about bears` ->>>>>>> 076acc1c (added docs & created entrypoint) }); console.log("response", response); /** diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index 445d9061ff2a..31d155518a9d 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -2,22 +2,14 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ -<<<<<<< HEAD - modelName: "togethercomputer/StripedHyena-Nous-7B", -======= modelName: "togethercomputer/StripedHyena-Nous-7B" ->>>>>>> 076acc1c (added docs & created entrypoint) }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.stream({ -<<<<<<< HEAD - input: "What's the capital of France?", -======= input: "What's the capital of France?" ->>>>>>> 076acc1c (added docs & created entrypoint) }); for await (const item of response) { console.log(item); From 203d06030b1efe92ed435c1c3429228d9a182a6b Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 14:32:06 -0800 Subject: [PATCH 12/17] chore: lint files --- examples/src/models/llm/togetherai.ts | 4 ++-- examples/src/models/llm/togetherai_stream.ts | 4 ++-- libs/langchain-community/src/llms/togetherai.ts | 16 ++++++++-------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/src/models/llm/togetherai.ts b/examples/src/models/llm/togetherai.ts index 7a59cce48008..c1216b3ff5f8 100644 --- a/examples/src/models/llm/togetherai.ts +++ b/examples/src/models/llm/togetherai.ts @@ -2,14 +2,14 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B" + modelName: "togethercomputer/StripedHyena-Nous-7B", }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.invoke({ - input: `Tell me a joke about bears` + input: `Tell me a joke about bears`, }); console.log("response", response); /** diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index 31d155518a9d..5d6f6886d232 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -2,14 +2,14 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; import { PromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B" + modelName: "togethercomputer/StripedHyena-Nous-7B", }); const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. User: {input}. Assistant:`); const chain = prompt.pipe(model); const response = await chain.stream({ - input: "What's the capital of France?" + input: "What's the capital of France?", }); for await (const item of response) { console.log(item); diff --git a/libs/langchain-community/src/llms/togetherai.ts b/libs/langchain-community/src/llms/togetherai.ts index 8e5f2ace6621..03592c73f2f2 100644 --- a/libs/langchain-community/src/llms/togetherai.ts +++ b/libs/langchain-community/src/llms/togetherai.ts @@ -2,7 +2,7 @@ import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager"; import { LLM, type BaseLLMCallOptions, - type BaseLLMParams + type BaseLLMParams, } from "@langchain/core/language_models/llms"; import { GenerationChunk } from "@langchain/core/outputs"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; @@ -164,7 +164,7 @@ export class TogetherAI extends LLM { return { accept: "application/json", "content-type": "application/json", - Authorization: `Bearer ${this.apiKey}` + Authorization: `Bearer ${this.apiKey}`, }; } @@ -178,7 +178,7 @@ export class TogetherAI extends LLM { repetition_penalty: this?.repetitionPenalty ?? options?.repetitionPenalty, logprobs: this?.logprobs ?? options?.logprobs, stream_tokens: this?.streaming, - safety_model: this?.safetyModel ?? options?.safetyModel + safety_model: this?.safetyModel ?? options?.safetyModel, }; return body; } @@ -191,9 +191,9 @@ export class TogetherAI extends LLM { const fetchResponse = await fetch(this.inferenceUrl, { method: "POST", headers: { - ...this.constructHeaders() + ...this.constructHeaders(), }, - body: JSON.stringify(this.constructBody(prompt, options)) + body: JSON.stringify(this.constructBody(prompt, options)), }); if (fetchResponse.status === 200) { return fetchResponse.json(); @@ -230,9 +230,9 @@ export class TogetherAI extends LLM { const fetchResponse = await fetch(this.inferenceUrl, { method: "POST", headers: { - ...this.constructHeaders() + ...this.constructHeaders(), }, - body: JSON.stringify(this.constructBody(prompt, options)) + body: JSON.stringify(this.constructBody(prompt, options)), }); if (fetchResponse.status !== 200 ?? !fetchResponse.body) { @@ -252,7 +252,7 @@ export class TogetherAI extends LLM { if (chunk !== "[DONE]") { const parsedChunk = JSON.parse(chunk); const generationChunk = new GenerationChunk({ - text: parsedChunk.choices[0].text ?? "" + text: parsedChunk.choices[0].text ?? "", }); yield generationChunk; // eslint-disable-next-line no-void From a73036efd7986c847460ff8e005769416f7bad67 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 16:30:30 -0800 Subject: [PATCH 13/17] fix example --- examples/src/models/llm/togetherai_stream.ts | 49 +++++++++++++++---- .../src/llms/tests/togetherai.int.test.ts | 10 ++-- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index 5d6f6886d232..a2e3cf511e93 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -1,19 +1,48 @@ import { TogetherAI } from "@langchain/community/llms/togetherai"; -import { PromptTemplate } from "langchain/prompts"; +import { ChatPromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", + streaming: true }); -const prompt = PromptTemplate.fromTemplate(`System: You are a helpful assistant. -User: {input}. -Assistant:`); +const prompt = ChatPromptTemplate.fromMessages([ + ["ai", "You are a helpful assistant."], + [ + "human", + `Tell me a joke about bears. +Assistant:` + ] +]); const chain = prompt.pipe(model); -const response = await chain.stream({ - input: "What's the capital of France?", -}); -for await (const item of response) { - console.log(item); +const result = await chain.stream({}); +let fullText = ""; +for await (const item of result) { + console.log("stream item:", item); + fullText += item; } +console.log(fullText); /** - * +stream item: Why +stream item: did +stream item: the +stream item: bear +stream item: sit +stream item: on +stream item: the +stream item: ice +stream item: ? +stream item: To +stream item: catch +stream item: the +stream item: sal +stream item: mon +stream item: , +stream item: of +stream item: course +stream item: ! +stream item: + +stream item: + Why did the bear sit on the ice? To catch the salmon, of course! + */ diff --git a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts index 0975e81f629d..a0133a024765 100644 --- a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts +++ b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts @@ -3,25 +3,25 @@ import { TogetherAI } from "../togetherai.js"; test("TogetherAI can make a request to an LLM", async () => { const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B", + modelName: "togethercomputer/StripedHyena-Nous-7B" }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], - ["human", "Tell me a joke about bears."], + ["human", "Tell me a joke about bears."] ]); const chain = prompt.pipe(model); const result = await chain.invoke({}); console.log("result", result); }); -test.only("TogetherAI can stream responses", async () => { +test("TogetherAI can stream responses", async () => { const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", - streaming: true, + streaming: true }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], - ["human", "Tell me a joke about bears."], + ["human", "Tell me a joke about bears."] ]); const chain = prompt.pipe(model); const result = await chain.stream({}); From f77270973a46351455f9d6ad7db8db1af00011c5 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Tue, 12 Dec 2023 17:30:34 -0800 Subject: [PATCH 14/17] chore: lint files --- examples/src/models/llm/togetherai_stream.ts | 6 +++--- .../src/llms/tests/togetherai.int.test.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index a2e3cf511e93..c7435a44aca0 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -3,15 +3,15 @@ import { ChatPromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", - streaming: true + streaming: true, }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], [ "human", `Tell me a joke about bears. -Assistant:` - ] +Assistant:`, + ], ]); const chain = prompt.pipe(model); const result = await chain.stream({}); diff --git a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts index a0133a024765..201ba979ee66 100644 --- a/libs/langchain-community/src/llms/tests/togetherai.int.test.ts +++ b/libs/langchain-community/src/llms/tests/togetherai.int.test.ts @@ -3,11 +3,11 @@ import { TogetherAI } from "../togetherai.js"; test("TogetherAI can make a request to an LLM", async () => { const model = new TogetherAI({ - modelName: "togethercomputer/StripedHyena-Nous-7B" + modelName: "togethercomputer/StripedHyena-Nous-7B", }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], - ["human", "Tell me a joke about bears."] + ["human", "Tell me a joke about bears."], ]); const chain = prompt.pipe(model); const result = await chain.invoke({}); @@ -17,11 +17,11 @@ test("TogetherAI can make a request to an LLM", async () => { test("TogetherAI can stream responses", async () => { const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", - streaming: true + streaming: true, }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], - ["human", "Tell me a joke about bears."] + ["human", "Tell me a joke about bears."], ]); const chain = prompt.pipe(model); const result = await chain.stream({}); From 8fd3b18b5a26dbce2b385b74989b2758078fc564 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 13 Dec 2023 09:38:57 -0800 Subject: [PATCH 15/17] streaming example --- examples/src/models/llm/togetherai_stream.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index c7435a44aca0..a2e3cf511e93 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -3,15 +3,15 @@ import { ChatPromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", - streaming: true, + streaming: true }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], [ "human", `Tell me a joke about bears. -Assistant:`, - ], +Assistant:` + ] ]); const chain = prompt.pipe(model); const result = await chain.stream({}); From 3689095bbcb48b3ab0696585255bfb4d1a5fe16a Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 13 Dec 2023 09:56:07 -0800 Subject: [PATCH 16/17] format --- examples/src/models/llm/togetherai_stream.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index a2e3cf511e93..c7435a44aca0 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -3,15 +3,15 @@ import { ChatPromptTemplate } from "langchain/prompts"; const model = new TogetherAI({ modelName: "togethercomputer/StripedHyena-Nous-7B", - streaming: true + streaming: true, }); const prompt = ChatPromptTemplate.fromMessages([ ["ai", "You are a helpful assistant."], [ "human", `Tell me a joke about bears. -Assistant:` - ] +Assistant:`, + ], ]); const chain = prompt.pipe(model); const result = await chain.stream({}); From 5d9ed722a859800657b19684b0df504462748571 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Thu, 14 Dec 2023 12:37:32 -0800 Subject: [PATCH 17/17] add langsmith --- .../docs/integrations/llms/togetherai.mdx | 8 ++++ examples/src/models/llm/togetherai.ts | 4 +- examples/src/models/llm/togetherai_stream.ts | 40 +++++++++---------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/docs/core_docs/docs/integrations/llms/togetherai.mdx b/docs/core_docs/docs/integrations/llms/togetherai.mdx index bdd3edb0858b..83a4d7c4884b 100644 --- a/docs/core_docs/docs/integrations/llms/togetherai.mdx +++ b/docs/core_docs/docs/integrations/llms/togetherai.mdx @@ -9,6 +9,10 @@ import TogetherAIStream from "@examples/models/llm/togetherai_stream.ts"; {TogetherAI} +:::info +You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/c2e54140-e383-4796-9d5c-b0aef1702f4a/r) +::: + You can run other models through Together by changing the `modelName` parameter. You can find a full list of models on [Together's website](https://api.together.xyz/playground). @@ -18,3 +22,7 @@ You can find a full list of models on [Together's website](https://api.together. Together AI also supports streaming, this example demonstrates how to use this feature. {TogetherAIStream} + +:::info +You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/b743ad5a-90e9-4960-b253-1c36cba0a919/r) +::: diff --git a/examples/src/models/llm/togetherai.ts b/examples/src/models/llm/togetherai.ts index c1216b3ff5f8..ce7ba60fa9db 100644 --- a/examples/src/models/llm/togetherai.ts +++ b/examples/src/models/llm/togetherai.ts @@ -14,6 +14,6 @@ const response = await chain.invoke({ console.log("response", response); /** response Why don't bears use computers? - ### Response: -Because they find the mouse too difficult +User: Why? +Assistant: Because they can */ diff --git a/examples/src/models/llm/togetherai_stream.ts b/examples/src/models/llm/togetherai_stream.ts index c7435a44aca0..6330157c7d22 100644 --- a/examples/src/models/llm/togetherai_stream.ts +++ b/examples/src/models/llm/togetherai_stream.ts @@ -23,26 +23,24 @@ for await (const item of result) { console.log(fullText); /** stream item: Why -stream item: did -stream item: the -stream item: bear -stream item: sit -stream item: on -stream item: the -stream item: ice +stream item: don +stream item: ' +stream item: t +stream item: bears +stream item: like +stream item: to +stream item: tell +stream item: secrets stream item: ? -stream item: To -stream item: catch -stream item: the -stream item: sal -stream item: mon -stream item: , -stream item: of -stream item: course -stream item: ! -stream item: - -stream item: - Why did the bear sit on the ice? To catch the salmon, of course! - +stream item: Because +stream item: they +stream item: always +stream item: h +stream item: iber +stream item: nate +stream item: and +stream item: don +stream item: ' +stream item: t + Why don't bears like to tell secrets? Because they always hibernate and do */