kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
old-agentic-v1^2
rodzic
6e823e7d97
commit
c932ae50d1
|
@ -0,0 +1,83 @@
|
||||||
|
import { OpenAIClient } from '@agentic/openai-fetch'
|
||||||
|
import 'dotenv/config'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
import {
|
||||||
|
Agentic,
|
||||||
|
HumanFeedbackMechanismTwilio,
|
||||||
|
SearchAndCrawlTool,
|
||||||
|
WeatherTool,
|
||||||
|
withHumanFeedback
|
||||||
|
} from '@/index'
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! })
|
||||||
|
const agentic = new Agentic({ openai })
|
||||||
|
|
||||||
|
const topic = process.argv[2] || 'OpenAI'
|
||||||
|
|
||||||
|
const res0 = await withHumanFeedback(
|
||||||
|
agentic
|
||||||
|
.gpt4({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'system',
|
||||||
|
content: `You are a MckInsey analyst who is an expert at writing executive summaries.`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: `What are the 3 most important questions we would need to answer in order to have a thorough understanding of this topic: {{topic}}? Be concise but creative in your questions, and make sure to capture the true essence of the topic.`
|
||||||
|
}
|
||||||
|
],
|
||||||
|
model: 'gpt-4',
|
||||||
|
temperature: 1.0
|
||||||
|
})
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
topic: z.string()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.output(z.array(z.string()).describe('question')),
|
||||||
|
{
|
||||||
|
type: 'selectN',
|
||||||
|
mechanism: HumanFeedbackMechanismTwilio
|
||||||
|
}
|
||||||
|
).callWithMetadata({ topic })
|
||||||
|
|
||||||
|
console.log()
|
||||||
|
console.log()
|
||||||
|
console.log(`Questions: ${res0.result}`)
|
||||||
|
console.log()
|
||||||
|
console.log()
|
||||||
|
|
||||||
|
const questions: string[] = (res0.metadata.feedback as any)!.selected
|
||||||
|
|
||||||
|
const res = await agentic
|
||||||
|
.gpt4({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: 'system',
|
||||||
|
content: `You are a MckInsey analyst who is an expert at writing executive summaries.`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: `Write a thorough executive summary on this topic: {{topic}}.
|
||||||
|
In order to do this, you will need to answer the following questions: \n{{#questions}}- {{.}}\n{{/questions}}`
|
||||||
|
}
|
||||||
|
],
|
||||||
|
model: 'gpt-4-32k'
|
||||||
|
})
|
||||||
|
.tools([new SearchAndCrawlTool(), new WeatherTool()])
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
topic: z.string(),
|
||||||
|
questions: z.array(z.string())
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.call({ topic, questions })
|
||||||
|
|
||||||
|
console.log('\n\n\n')
|
||||||
|
console.log(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
|
@ -1,7 +1,11 @@
|
||||||
|
import { TimeoutError as KyTimeoutError } from 'ky'
|
||||||
|
import { TimeoutError } from 'p-timeout'
|
||||||
import type { Jsonifiable } from 'type-fest'
|
import type { Jsonifiable } from 'type-fest'
|
||||||
import type { ZodError } from 'zod'
|
import type { ZodError } from 'zod'
|
||||||
import { ValidationError, fromZodError } from 'zod-validation-error'
|
import { ValidationError, fromZodError } from 'zod-validation-error'
|
||||||
|
|
||||||
|
export { TimeoutError, KyTimeoutError }
|
||||||
|
|
||||||
export type ErrorOptions = {
|
export type ErrorOptions = {
|
||||||
/** HTTP status code for the error. */
|
/** HTTP status code for the error. */
|
||||||
status?: number
|
status?: number
|
||||||
|
|
|
@ -216,6 +216,11 @@ export abstract class HumanFeedbackMechanism<
|
||||||
choices.push(HumanFeedbackUserActions.Abort)
|
choices.push(HumanFeedbackUserActions.Abort)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._agentic.logger.info(
|
||||||
|
choices,
|
||||||
|
`>>> Human feedback ${this.constructor.name} ${msg}`
|
||||||
|
)
|
||||||
|
|
||||||
const choice =
|
const choice =
|
||||||
choices.length === 1
|
choices.length === 1
|
||||||
? HumanFeedbackUserActions.Select
|
? HumanFeedbackUserActions.Select
|
||||||
|
@ -291,6 +296,10 @@ export abstract class HumanFeedbackMechanism<
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._agentic.logger.info(
|
||||||
|
feedback,
|
||||||
|
`<<< Human feedback ${this.constructor.name} ${msg}`
|
||||||
|
)
|
||||||
return feedback as FeedbackTypeToMetadata<T>
|
return feedback as FeedbackTypeToMetadata<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,6 +192,12 @@ export abstract class BaseTask<
|
||||||
} else if (err instanceof errors.HumanFeedbackDeclineError) {
|
} else if (err instanceof errors.HumanFeedbackDeclineError) {
|
||||||
ctx.retryMessage = err.message
|
ctx.retryMessage = err.message
|
||||||
return
|
return
|
||||||
|
} else if (
|
||||||
|
err instanceof errors.KyTimeoutError ||
|
||||||
|
err instanceof errors.TimeoutError ||
|
||||||
|
(err as any).name === 'TimeoutError'
|
||||||
|
) {
|
||||||
|
// TODO
|
||||||
} else if ((err.cause as any)?.code === 'UND_ERR_HEADERS_TIMEOUT') {
|
} else if ((err.cause as any)?.code === 'UND_ERR_HEADERS_TIMEOUT') {
|
||||||
// TODO: This is a pretty common OpenAI error, and I think it either has
|
// TODO: This is a pretty common OpenAI error, and I think it either has
|
||||||
// to do with OpenAI's servers being flaky or the combination of Node.js
|
// to do with OpenAI's servers being flaky or the combination of Node.js
|
||||||
|
|
Ładowanie…
Reference in New Issue