1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import process from "node:process";
export async function getModelBuilder(spec: {
type?: "llm" | "chat" | "embedding";
provider?: "openai" | "huggingface";
} = { type: "llm", provider: "openai" }, options?: any) {
const { extend, cond, matches, invoke } = await import("npm:lodash-es");
// Set up LangSmith tracer
const { Client } = await import("npm:langsmith");
const { LangChainTracer } = await import("npm:langchain/callbacks");
const client = new Client({
apiUrl: "https://api.smith.langchain.com",
apiKey: process.env.LANGSMITH,
});
const tracer = new LangChainTracer({ client });
const callbacks = options?.verbose !== false ? [tracer] : [];
// Set up API key for each providers
const args = extend({ callbacks }, options);
if (spec?.provider === "openai")
args.openAIApiKey = process.env.OPENAI;
else if (spec?.provider === "huggingface")
args.apiKey = process.env.HUGGINGFACE;
// Populate model builders
const setup = cond([
[
matches({ type: "llm", provider: "openai" }),
async () => {
const { OpenAI } = await import("npm:langchain/llms/openai");
return new OpenAI(args);
},
],
[
matches({ type: "chat", provider: "openai" }),
async () => {
const { ChatOpenAI } = await import("npm:langchain/chat_models/openai");
return new ChatOpenAI(args);
},
],
[
matches({ type: "embedding", provider: "openai" }),
async () => {
const { OpenAIEmbeddings } = await import(
"npm:langchain/embeddings/openai"
);
return new OpenAIEmbeddings(args);
},
],
[
matches({ type: "llm", provider: "huggingface" }),
async () => {
await import("npm:@huggingface/inference");
const { HuggingFaceInference } = await import("npm:langchain/llms/hf");
return new HuggingFaceInference(args);
},
],
[
matches({ type: "embedding", provider: "huggingface" }),
async () => {
await import("npm:@huggingface/inference");
const { HuggingFaceInferenceEmbeddings } = await import(
"npm:langchain/embeddings/hf"
);
return new HuggingFaceInferenceEmbeddings(args);
},
],
]);
// Return function to prevent "Serialization Error"
return () => setup(spec);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
1
stevekrouse avatar

Do you intend for others to be able to use @me.secrets.LANGSMITH if they call your function via the Run API?

If not, I'd suggest making it an argument so other users can pass their own.

October 23, 2023