From a6591106fa970aba8e4b3a0d1a0de7a7c7c2a7f2 Mon Sep 17 00:00:00 2001 From: Jim Fisher Date: Mon, 10 Apr 2023 19:12:27 +0200 Subject: [PATCH 1/2] More LLM hello world examples Add OpenAI Chat, Replicate, and HuggingFace Also nest these examples under llms/, mirroring source layout Also improve prompt - the bare question usually gives nonsense --- .../cohere.ts} | 5 ++--- examples/src/llms/hf.ts | 14 ++++++++++++++ examples/src/llms/openai-chat.ts | 17 +++++++++++++++++ .../openai.ts} | 5 ++--- examples/src/llms/replicate.ts | 11 +++++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) rename examples/src/{llm_cohere_concurrency.ts => llms/cohere.ts} (63%) create mode 100644 examples/src/llms/hf.ts create mode 100644 examples/src/llms/openai-chat.ts rename examples/src/{llm_openai_concurrency.ts => llms/openai.ts} (65%) create mode 100644 examples/src/llms/replicate.ts diff --git a/examples/src/llm_cohere_concurrency.ts b/examples/src/llms/cohere.ts similarity index 63% rename from examples/src/llm_cohere_concurrency.ts rename to examples/src/llms/cohere.ts index 71096a3a5e08..f4a7a7eb15b9 100644 --- a/examples/src/llm_cohere_concurrency.ts +++ b/examples/src/llms/cohere.ts @@ -4,12 +4,11 @@ export const run = async () => { const model = new Cohere({ temperature: 0.7, verbose: true, - maxConcurrency: 1, maxTokens: 20, maxRetries: 5, }); const res = await model.call( - "What would be a good company name a company that makes colorful socks?" + "Question: What would be a good company name a company that makes colorful socks?\nAnswer:" ); - console.log("hello", { res }); + console.log({ res }); }; diff --git a/examples/src/llms/hf.ts b/examples/src/llms/hf.ts new file mode 100644 index 000000000000..e4b4d83f147d --- /dev/null +++ b/examples/src/llms/hf.ts @@ -0,0 +1,14 @@ +import { HuggingFaceInference } from "langchain/llms"; + +export const run = async () => { + const model = new HuggingFaceInference({ + model: "gpt2", + temperature: 0.7, + verbose: true, + maxTokens: 50, + }); + const res = await model.call( + "Question: What would be a good company name a company that makes colorful socks?\nAnswer:" + ); + console.log({ res }); +}; diff --git a/examples/src/llms/openai-chat.ts b/examples/src/llms/openai-chat.ts new file mode 100644 index 000000000000..24f24c8f2a7d --- /dev/null +++ b/examples/src/llms/openai-chat.ts @@ -0,0 +1,17 @@ +import { OpenAIChat } from "langchain/llms"; + +export const run = async () => { + const model = new OpenAIChat({ + prefixMessages: [ + { + role: "system", + content: "You are a helpful assistant that answers in pirate language", + }, + ], + maxTokens: 50, + }); + const res = await model.call( + "What would be a good company name a company that makes colorful socks?" + ); + console.log({ res }); +}; diff --git a/examples/src/llm_openai_concurrency.ts b/examples/src/llms/openai.ts similarity index 65% rename from examples/src/llm_openai_concurrency.ts rename to examples/src/llms/openai.ts index 1905a32e09bf..6f360e1489ed 100644 --- a/examples/src/llm_openai_concurrency.ts +++ b/examples/src/llms/openai.ts @@ -5,12 +5,11 @@ export const run = async () => { modelName: "gpt-4", temperature: 0.7, verbose: true, - maxConcurrency: 1, maxTokens: 1000, maxRetries: 5, }); const res = await model.call( - "What would be a good company name a company that makes colorful socks?" + "Question: What would be a good company name a company that makes colorful socks?\nAnswer:" ); - console.log("hello", { res }); + console.log({ res }); }; diff --git a/examples/src/llms/replicate.ts b/examples/src/llms/replicate.ts new file mode 100644 index 000000000000..28ae1e883b04 --- /dev/null +++ b/examples/src/llms/replicate.ts @@ -0,0 +1,11 @@ +import { Replicate } from "langchain/llms"; + +export const run = async () => { + const model = new Replicate({ + model: "replicate/flan-t5-xl:3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523" + }); + const res = await model.call( + "Question: What would be a good company name a company that makes colorful socks?\nAnswer:" + ); + console.log({ res }); +}; From 86df1991c0328a45acf7e48bec6d10b43b9f3dca Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 11 Apr 2023 09:10:30 +0100 Subject: [PATCH 2/2] Fix for new paths --- examples/src/llms/hf.ts | 2 +- examples/src/llms/openai-chat.ts | 2 +- examples/src/llms/replicate.ts | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/src/llms/hf.ts b/examples/src/llms/hf.ts index e4b4d83f147d..36164818326f 100644 --- a/examples/src/llms/hf.ts +++ b/examples/src/llms/hf.ts @@ -1,4 +1,4 @@ -import { HuggingFaceInference } from "langchain/llms"; +import { HuggingFaceInference } from "langchain/llms/hf"; export const run = async () => { const model = new HuggingFaceInference({ diff --git a/examples/src/llms/openai-chat.ts b/examples/src/llms/openai-chat.ts index 24f24c8f2a7d..3e536a683e0c 100644 --- a/examples/src/llms/openai-chat.ts +++ b/examples/src/llms/openai-chat.ts @@ -1,4 +1,4 @@ -import { OpenAIChat } from "langchain/llms"; +import { OpenAIChat } from "langchain/llms/openai"; export const run = async () => { const model = new OpenAIChat({ diff --git a/examples/src/llms/replicate.ts b/examples/src/llms/replicate.ts index 28ae1e883b04..cce3b6c02af5 100644 --- a/examples/src/llms/replicate.ts +++ b/examples/src/llms/replicate.ts @@ -1,8 +1,9 @@ -import { Replicate } from "langchain/llms"; +import { Replicate } from "langchain/llms/replicate"; export const run = async () => { const model = new Replicate({ - model: "replicate/flan-t5-xl:3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523" + model: + "replicate/flan-t5-xl:3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523", }); const res = await model.call( "Question: What would be a good company name a company that makes colorful socks?\nAnswer:"