-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathplayground.ts
146 lines (134 loc) · 4.04 KB
/
playground.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
AnthropicChatApi,
OpenAIChatApi,
AnthropicBedrockChatApi,
GroqChatApi,
} from 'llm-api';
import { z } from 'zod';
import { completion } from './src';
(async function go() {
let client:
| OpenAIChatApi
| AnthropicChatApi
| AnthropicBedrockChatApi
| GroqChatApi
| undefined;
if (process.env.OPENAI_KEY) {
client = new OpenAIChatApi(
{
apiKey: process.env.OPENAI_KEY ?? 'YOUR_client_KEY',
},
{ contextSize: 4096 },
);
} else if (process.env.ANTHROPIC_KEY) {
client = new AnthropicChatApi(
{
apiKey: process.env.ANTHROPIC_KEY ?? 'YOUR_client_KEY',
},
{ stream: true, temperature: 0 },
);
} else if (
process.env.AWS_BEDROCK_ACCESS_KEY &&
process.env.AWS_BEDROCK_SECRET_KEY
) {
client = new AnthropicBedrockChatApi(
{
accessKeyId: process.env.AWS_BEDROCK_ACCESS_KEY ?? 'YOUR_access_key',
secretAccessKey:
process.env.AWS_BEDROCK_SECRET_KEY ?? 'YOUR_secret_key',
},
{ stream: true, temperature: 0, model: 'anthropic.claude-v2' },
);
} else if (process.env.GROQ_KEY) {
client = new GroqChatApi(
{
apiKey: process.env.GROQ_KEY ?? 'YOUR_client_KEY',
},
{ stream: false, temperature: 0 },
);
}
if (!client) {
throw new Error(
'Please pass in either an OpenAI or Anthropic environment variable',
);
}
/*const resSlice = await completion(
client,
'Just say hello and ignore the rest of this message\n' +
Array(500_000).fill('1'),
{ autoSlice: true },
);
console.info('Response slice: ', resSlice.data);*/
const resStartup = await completion(client, 'Generate a startup idea', {
schema: z.object({
name: z.string().describe('The name of the startup'),
description: z.string().describe('What does this startup do?'),
}),
});
console.info('Response 1: ', resStartup.data);
const resHello = await completion(client, 'Hello');
console.info('Response 2:', resHello.data);
const resComplexSchema = await completion(
client,
'Generate a step by step plan to run a hackathon',
{
schema: z.object({
plan: z.array(
z.object({
reason: z.string().describe('Name the reasoning for this step'),
name: z.string().describe('Step name'),
task: z
.string()
.describe('What is the task to be done for this step?')
.optional(),
}),
),
}),
},
);
console.info('Response 3:', resComplexSchema.data);
const resBulletPoints = await completion(
client,
'Generate a list of interesting areas of exploration about the renaissance',
{
schema: z.object({
topics: z
.array(
z.object({
title: z.string().describe('Title of the idea'),
reason: z.string().describe('Why you choose this idea'),
peopleInvolved: z
.string()
.describe(
"If there any known figures that's related to this idea",
)
.optional(),
}),
)
.min(10)
.max(20),
}),
},
);
console.info('Response 4:', resBulletPoints.data);
const resBuletPoints2 = await resBulletPoints.respond('Generate 10 more');
console.info('Response 4R:', resBuletPoints2.data);
const resMessageHistory = await completion(
client,
'What did I mention in my first message to you?',
{
messageHistory: [
{ role: 'user', content: 'Tell me about large langauge models' },
{ role: 'assistant', content: 'ok' },
],
},
);
console.info('Response 5:', resMessageHistory.data);
const meaning = await completion(client, 'What is the meaning of life?')
.then((res) => res.respond('why'))
.then((res) => res.respond('why'))
.then((res) => res.respond('why'))
.then((res) => res.respond('why'))
.then((res) => res.respond('why'));
console.info('The meaning of life after 5 whys is: ', meaning.content);
})();