-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.ts
55 lines (52 loc) · 1.72 KB
/
utils.ts
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
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
import {
MessageContent,
MessageContentComplex,
} from "@langchain/core/messages";
import { initChatModel } from "langchain/chat_models/universal";
/**
* Helper function to extract text content from a complex message.
*
* @param content - The complex message content to process
* @returns The extracted text content
*/
function getSingleTextContent(content: MessageContentComplex) {
if (content?.type === "text") {
return content.text;
} else if (content.type === "array") {
return content.content.map(getSingleTextContent).join(" ");
}
return "";
}
/**
* Helper function to extract text content from various message types.
*
* @param content - The message content to process
* @returns The extracted text content
*/
export function getTextContent(content: MessageContent): string {
if (typeof content === "string") {
return content;
} else if (Array.isArray(content)) {
return content.map(getSingleTextContent).join(" ");
}
return "";
}
/**
* Load a chat model from a fully specified name.
* @param fullySpecifiedName - String in the format 'provider/model' or 'provider/account/provider/model'.
* @returns A Promise that resolves to a BaseChatModel instance.
*/
export async function loadChatModel(
fullySpecifiedName: string,
): Promise<BaseChatModel> {
const index = fullySpecifiedName.indexOf("/");
if (index === -1) {
// If there's no "/", assume it's just the model
return await initChatModel(fullySpecifiedName);
} else {
const provider = fullySpecifiedName.slice(0, index);
const model = fullySpecifiedName.slice(index + 1);
return await initChatModel(model, { modelProvider: provider });
}
}