-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanthropic.mts
227 lines (204 loc) · 10.8 KB
/
anthropic.mts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Anthropic from "@anthropic-ai/sdk"
import {LlmInterface} from "./llmInterface.mjs"
import {readConfig} from "./utils.mjs"
import {Message, MessageCreateParams, RawMessageStreamEvent, Tool} from "@anthropic-ai/sdk/resources/messages.mjs"
import {Stream} from "@anthropic-ai/sdk/streaming.mjs"
import {prompts} from "./prompts.mjs"
import {maskSensitiveInfo} from "./utils.mjs" // Assuming this function exists in utils.mjs
type modelType = keyof typeof modelMapping
const modelMapping = {
"claude-3-haiku": 'claude-3-haiku-20240307',
"claude-3.5-haiku": 'claude-3-5-haiku-20241022',
"claude-3.5-haiku-latest": 'claude-3-5-haiku-latest',
'claude-3-sonnet': 'claude-3-sonnet-20240229',
'claude-3-opus': 'claude-3-opus-20240229',
'claude-3.5-sonnet': 'claude-3-5-sonnet-20241022',
'claude-3.5-sonnet-legacy': 'claude-3-5-sonnet-20240620',
'claude-3.5-sonnet-latest': 'claude-3-5-sonnet-latest',
'haiku-3': 'claude-3-haiku-20240307',
'haiku-3.5': 'claude-3-5-haiku-20241022',
'haiku-3.5-latest': 'claude-3-5-haiku-latest',
'sonnet-3': 'claude-3-sonnet-20240229',
'opus-3': 'claude-3-opus-20240229',
'sonnet-3.5': 'claude-3-5-sonnet-20241022',
'sonnet-3.5-legacy': 'claude-3-5-sonnet-20240620',
'sonnet-3.5-latest': 'claude-3-5-sonnet-latest',
}
export class AnthropicInterface implements LlmInterface {
private getModelId(model: modelType): string {
return modelMapping[model]
}
private getClient(isVerbose: boolean) {
const config = readConfig()
const client = new Anthropic({
apiKey: config.ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY,
})
if (isVerbose) {
console.log("Anthropic client initialized")
}
return client
}
getName(): string {
return "Anthropic"
}
async listModels(isVerbose: boolean): Promise<string[]> {
if (isVerbose) {
console.log("Available Anthropic models:", maskSensitiveInfo(JSON.stringify(modelMapping, null, 2)))
}
return Object.keys(modelMapping)
}
async inferProjectDirectory(directoryStructure: string, allowStreaming: boolean,
isVerbose: boolean, userExpertise?: string, modelName?: string):
Promise<string | undefined> {
const model = this.getModel(modelName)
const systemPrompt = `${prompts.commonSystemPrompt.prompt}\n${prompts.rootUnderstanding.prompt}`
const userPrompt = `<FileStructure>${JSON.stringify(directoryStructure)}</FileStructure>`
const tools: Tool | undefined = prompts.rootUnderstanding.params ? {
name: prompts.rootUnderstanding.params.name,
input_schema: {
type: "object",
properties: {
isMonorepo: {type: "boolean", description: prompts.rootUnderstanding.params.parameters.properties['isMonorepo'].description},
directories: {
type: "array",
items: {type: "string"},
description: prompts.rootUnderstanding.params.parameters.properties['directories'].description
},
programmingLanguage: {
type: "string",
description: prompts.rootUnderstanding.params.parameters.properties['programmingLanguage'].description
},
framework: {
type: "string",
description: prompts.rootUnderstanding.params.parameters.properties['framework'].description,
},
dependenciesFile: {
type: "string",
description: prompts.rootUnderstanding.params.parameters.properties['dependenciesFile'].description
},
lockFile: {
type: "string",
description: prompts.rootUnderstanding.params.parameters.properties['lockFile'].description
},
entryPointFile: {
type: "string",
description: prompts.rootUnderstanding.params.parameters.properties['entryPointFile'].description
},
workflow: {
type: "string",
description: prompts.rootUnderstanding.params.parameters.properties['workflow'].description
},
},
},
description: prompts.rootUnderstanding.params.description,
} : undefined
if (isVerbose) {
console.log("Inferring project directory structure")
console.log("Model:", model)
console.log("System prompt:", maskSensitiveInfo(systemPrompt))
console.log("User prompt:", maskSensitiveInfo(userPrompt))
console.log("Tools:", maskSensitiveInfo(JSON.stringify(tools, null, 2)))
}
return this.callApiAndReturnString(model, systemPrompt, userPrompt,
tools, allowStreaming, isVerbose, userExpertise) as Promise<string | undefined>
}
async inferDependency(dependencyFile: string, workflow: string, allowStreaming: boolean, isVerbose: boolean, userExpertise?: string, modelName?: string): Promise<string | undefined | AsyncIterable<string>> {
const model = this.getModel(modelName)
const systemPrompt = `${prompts.commonSystemPrompt.prompt}\n${prompts.dependencyUnderstanding.prompt}`
const userPrompt = `<DependencyFile>${JSON.stringify(dependencyFile)}</DependencyFile>\n<Workflow>${workflow}</Workflow> ${prompts.commonMarkdownPrompt.prompt}`
return this.callApiAndReturnString(model, systemPrompt, userPrompt, undefined, allowStreaming, isVerbose, userExpertise)
}
async inferCode(directoryStructure: string, allowStreaming: boolean,
isVerbose: boolean, userExpertise?: string, modelName?: string): Promise<string | undefined | AsyncIterable<string>> {
const model = this.getModel(modelName)
const systemPrompt = `${prompts.commonSystemPrompt.prompt}\n${prompts.codeUnderstanding.prompt}`
const userPrompt = `<Code>${JSON.stringify(directoryStructure)}</Code> ${prompts.commonMarkdownPrompt.prompt}`
return this.callApiAndReturnString(model, systemPrompt, userPrompt, undefined, allowStreaming, isVerbose, userExpertise)
}
async inferInterestingCode(directoryStructure: string, allowStreaming: boolean, isVerbose: boolean, userExpertise?: string, modelName?: string): Promise<string | undefined | AsyncIterable<string>> {
const model = this.getModel(modelName)
const systemPrompt = prompts.interestingCodeParts.prompt
const userPrompt = `<Code>${JSON.stringify(directoryStructure)}</Code> ${prompts.commonMarkdownPrompt.prompt}`
return this.callApiAndReturnString(model, systemPrompt, userPrompt, undefined, allowStreaming, isVerbose, userExpertise)
}
async generateReadme(directoryStructure: string, dependencyInference: string, codeInference: string, allowStreaming: boolean, isVerbose: boolean, userExpertise?: string, modelName?: string): Promise<string | undefined | AsyncIterable<string>> {
const model = this.getModel(modelName)
const systemPrompt = prompts.readmePrompt.prompt
const userPrompt = `<DirectoryStructure>${JSON.stringify(directoryStructure)}</DirectoryStructure>\n<DependencyInference>${JSON.stringify(dependencyInference)}</DependencyInference>\n<CodeInference>${JSON.stringify(codeInference)}</CodeInference> ${prompts.commonMarkdownPrompt.prompt}`
return this.callApiAndReturnString(model, systemPrompt, userPrompt, undefined, allowStreaming, isVerbose, userExpertise)
}
private getModel(modelName?: string): string {
const config = readConfig()
const selectedModel = modelName || config.DEFAULT_ANTHROPIC_MODEL || process.env.DEFAULT_ANTHROPIC_MODEL || "claude-3-opus-20240229"
return this.getModelId(selectedModel as modelType)
}
private async callApiAndReturnString(modelId: string, systemPrompt: string,
userPrompt: string, tools?: Tool, allowStreaming: boolean = false,
isVerbose: boolean = false, userExpertise?: string):
Promise<string | undefined | AsyncIterable<string>> {
const client = this.getClient(isVerbose)
let finalSystemPrompt = systemPrompt
if (userExpertise) {
finalSystemPrompt += `\n<Expertise>${JSON.stringify(userExpertise)}</Expertise>`
}
const messageConfig: MessageCreateParams = {
max_tokens: 8192,
model: modelId,
system: finalSystemPrompt,
messages: [
{
role: 'user',
content: userPrompt
}
],
stream: allowStreaming
}
if (tools) {
messageConfig.tools = [tools]
messageConfig.tool_choice = {type: 'tool', name: tools.name}
delete messageConfig.stream
allowStreaming = false
}
if (isVerbose) {
console.log("Sending request to Anthropic API")
console.log("Message config:", maskSensitiveInfo(JSON.stringify(messageConfig, null, 2)))
}
const response = await client.messages.create(messageConfig)
if (allowStreaming) {
const stream = response as Stream<RawMessageStreamEvent>
const streamedYield = this.convertStreamToStringStream(stream, isVerbose)
return streamedYield
} else {
const message = response as Message
if (isVerbose) {
console.log("Received response from Anthropic API")
console.log("Response:", maskSensitiveInfo(JSON.stringify(message, null, 2)))
}
if (message.stop_reason === 'tool_use') {
const toolContent = message.content.find(contentData => contentData.type === 'tool_use')
if (toolContent) {
return JSON.stringify(toolContent.input)
} else {
return ""
}
}
const data = message.content.filter(content => content.type === 'text').map(content => content.text).join("\n")
return data
}
}
private async * convertStreamToStringStream(response: Stream<RawMessageStreamEvent>,
isVerbose: boolean): AsyncIterable<string> {
for await (const chunk of response) {
if (chunk.type === 'content_block_delta') {
if (chunk.delta.type === 'text_delta') {
if (isVerbose) {
console.log("Received chunk:", maskSensitiveInfo(chunk.delta.text))
}
yield chunk.delta.text
}
}
yield ""
}
}
}
export default AnthropicInterface