-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanthropic-streaming.ts
81 lines (70 loc) · 2.43 KB
/
anthropic-streaming.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
import { anthropic } from '@ai-sdk/anthropic'
import { getSuiwareAiTools } from '@suiware/ai-tools'
import {
CoreMessage,
InvalidToolArgumentsError,
NoSuchToolError,
streamText,
ToolExecutionError,
} from 'ai'
import chalk from 'chalk'
import { configDotenv } from 'dotenv'
import * as readline from 'node:readline/promises'
configDotenv()
const terminal = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const messages: CoreMessage[] = []
const AGENT_NAME = 'Charlie'
async function main() {
process.stdout.write(
chalk.cyan(`\nThe agent is connected and awaiting your instructions...\n\n`)
)
while (true) {
const userInput = await terminal.question(chalk.green('You: '))
messages.push({ role: 'user', content: userInput })
const result = streamText({
model: anthropic('claude-3-5-sonnet-latest'),
messages,
tools: getSuiwareAiTools(),
maxSteps: 5,
system: `You are ${AGENT_NAME}, a financial assistant who manages user's portfolio on Sui blockchain network.
Answer very briefly and concisely. Every sentence of the answer should be on a separate line.
If user asks for balances, don't use the data from your memory and instead always request the balance tool.
If you don't know, don't make it up.`,
onError: ({ error }) => {
if (NoSuchToolError.isInstance(error)) {
process.stdout.write(chalk.red(`\nNo such tool: ${error.toolName}\n`))
} else if (InvalidToolArgumentsError.isInstance(error)) {
process.stdout.write(
chalk.red(
`\nInvalid arguments: ${error.toolName}: ${error.message}\n`
)
)
} else if (ToolExecutionError.isInstance(error)) {
process.stdout.write(
chalk.red(
`\nTool execution error: ${error.toolName}: ${error.message}\n`
)
)
} else {
process.stdout.write(
chalk.red(`\nUnknown error: ${(error as Error)?.message}\n`)
)
}
},
})
let fullResponse = ''
process.stdout.write(`\n${chalk.cyan(`${AGENT_NAME}: `)}`)
for await (const delta of result?.textStream ?? []) {
fullResponse += delta
process.stdout.write(delta)
}
process.stdout.write('\n\n')
messages.push({ role: 'assistant', content: fullResponse })
}
}
main().catch((error) => {
console.error(chalk.red('🚨 Fatal error:'), error)
})