chatgpt-api/examples/sentiment.ts

43 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

2023-05-27 00:38:55 +00:00
import dotenv from 'dotenv-safe'
import { OpenAIClient } from 'openai-fetch'
import { z } from 'zod'
import { Agentic } from '../src'
dotenv.config()
export async function sentimentAgent() {
const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! })
const $ = new Agentic({ openai })
const examples = [
{ input: 'The food was digusting', output: 'negative' },
{ input: 'We had a fantastic night', output: 'positive' },
{ input: 'Recommended', output: 'positive' },
{ input: 'The waiter was rude', output: 'negative' }
]
const example = await $.gpt4(
`You are a sentiment-labelling assistant. Label the following texts as positive or negative: {{texts}}`
)
.input(z.object({ texts: z.string().array() }))
.output(z.array(z.object({ text: z.string(), label: z.string() })))
.examples(examples)
// .assert((output) => output.filter(({ label }) =>
// !['positive', 'negative'].includes(label)
// ).length === 0
// })
.call({
texts: [
'I went to this place and it was just so awful.',
'I had a great time.',
'I had a terrible time.',
'I had a good time.'
]
})
console.log('example', example)
}
sentimentAgent()