-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.ts
78 lines (71 loc) · 2.42 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { ChatOpenAI } from "langchain/chat_models/openai"
import { HumanMessage, SystemMessage, AIMessage } from "langchain/schema"
require("dotenv").config()
// TODO: make a key at openrouter.ai/keys and put it in .env
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY
const OPENROUTER_BASE_URL =
process.env.OPENROUTER_BASE_URL || "https://openrouter.ai"
const chat = new ChatOpenAI(
{
modelName: "openai/gpt-3.5-turbo",
// modelName: "tiiuae/falcon-40b-instruct",
// modelName: "anthropic/claude-instant-v1",
// modelName: "anthropic/claude-2",
// modelName: "google/palm-2-chat-bison",
temperature: 0.8,
maxTokens: 300,
streaming: true,
openAIApiKey: OPENROUTER_API_KEY,
},
{
basePath: `${OPENROUTER_BASE_URL}/api/v1`,
baseOptions: {
headers: {
"HTTP-Referer": "https://localhost:3000/",
"X-Title": "Langchain.js Testing",
},
},
}
)
async function main() {
const response = await chat.call([
new SystemMessage(
"You are a helpful assistant that translates English to some other language, depending on the context."
),
...lotsOfMessages(2),
new HumanMessage(
"Translate: I am bouncy goofball who loves cookies and wants to go to Disneyland and ride the teacups. But one day I will be a real boy."
),
])
console.log(response)
}
// Helpers
function* lotsOfMessages(numMessages: number) {
const batch = [
new HumanMessage("Translate: I make cookies."),
new AIMessage("Je fais des biscuits."),
new HumanMessage("Translate: I am a human."),
new AIMessage("Je suis un humain."),
new HumanMessage("Translate: I am a robot."),
new AIMessage("Je suis un robot."),
new HumanMessage("Translate: I am a dog."),
new AIMessage("Je suis un chien."),
new HumanMessage("Translate: I am a cat."),
new AIMessage("Je suis un chat."),
new HumanMessage("Translate: I am a bird."),
new AIMessage("Je suis un oiseau."),
new HumanMessage("Translate: I am a fish."),
new AIMessage("Je suis un poisson."),
new HumanMessage("Translate: I am a horse."),
new AIMessage("Je suis un cheval."),
new HumanMessage("Translate: I am a cow."),
new AIMessage("Je suis une vache."),
new HumanMessage("Translate: I am a pig."),
new AIMessage("Je suis un cochon."),
]
for (let i = 0; i < numMessages; i++) {
yield batch[i % batch.length]
}
}
// Run
main().catch((e) => console.error(e))