kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: add Task.callWithMetadata
rodzic
fdb4361379
commit
e5134c2ea2
|
@ -13,12 +13,12 @@ export async function main() {
|
|||
)
|
||||
.input(z.object({ texts: z.string().array() }))
|
||||
.output(z.array(z.object({ text: z.string(), label: z.string() })))
|
||||
.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' }
|
||||
])
|
||||
// .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' }
|
||||
// ])
|
||||
.call({
|
||||
texts: [
|
||||
'I went to this place and it was just so awful.',
|
||||
|
|
22
readme.md
22
readme.md
|
@ -15,6 +15,7 @@
|
|||
- [Development](#development)
|
||||
- [Environment](#environment)
|
||||
- [Local Testing](#local-testing)
|
||||
- [Scratch](#scratch)
|
||||
- [License](#license)
|
||||
|
||||
## Intro
|
||||
|
@ -55,6 +56,27 @@ Ensure you have `REDIS_URL_TEST` set to a valid redis connection URL.
|
|||
pnpm test
|
||||
```
|
||||
|
||||
### Scratch
|
||||
|
||||
- `@agentic/core`
|
||||
- Task, Agentic, logging, caching, types, constants
|
||||
- `@agentic/human-feedback`
|
||||
- `@agentic/human-feedback-cli`
|
||||
- `@agentic/human-feedback-sms`
|
||||
- `@agentic/human-feedback-slack`
|
||||
- `@agentic/experimenation`
|
||||
- `@agentic/tools`
|
||||
- `@agentic/tools-serpapi`
|
||||
- `@agentic/tools-metaphor`
|
||||
- `@agentic/tools-browser`
|
||||
- `@agentic/tools-multion`
|
||||
- `@agentic/llms`
|
||||
- `@agentic/llms-openai`
|
||||
- `@agentic/llms-anthropic`
|
||||
- `@agentic/llms-huggingface`
|
||||
- `@agentic/agents`
|
||||
- `@agentic/cli`
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Travis Fischer](https://transitivebullsh.it)
|
||||
|
|
|
@ -30,6 +30,9 @@ async function ExampleLLMQuery({ texts }: { texts: string[] }) {
|
|||
>
|
||||
<System>You are an expert sentiment-labelling assistant</System>
|
||||
|
||||
{/* <ConversationHistory /> */}
|
||||
{/* <PineconeMemory /> */}
|
||||
|
||||
<User>
|
||||
Label the following texts as positive or negative:
|
||||
{/* {texts.map((text) => `- ${text}\n`)} */}
|
||||
|
|
20
src/llm.ts
20
src/llm.ts
|
@ -86,12 +86,6 @@ export abstract class BaseLLM<
|
|||
this._modelParams = { ...this._modelParams, ...params } as TModelParams
|
||||
return this
|
||||
}
|
||||
|
||||
// TODO
|
||||
// abstract stream({
|
||||
// input: TInput,
|
||||
// onProgress: types.ProgressFunction
|
||||
// }): Promise<TOutput>
|
||||
}
|
||||
|
||||
export abstract class BaseChatModel<
|
||||
|
@ -117,9 +111,9 @@ export abstract class BaseChatModel<
|
|||
messages: types.ChatMessage[]
|
||||
): Promise<types.BaseChatCompletionResponse<TChatCompletionResponse>>
|
||||
|
||||
override async call(
|
||||
protected override async _call(
|
||||
input?: types.ParsedData<TInput>
|
||||
): Promise<types.ParsedData<TOutput>> {
|
||||
): Promise<types.TaskResponse<TOutput>> {
|
||||
if (this._inputSchema) {
|
||||
const inputSchema =
|
||||
this._inputSchema instanceof z.ZodType
|
||||
|
@ -248,9 +242,15 @@ export abstract class BaseChatModel<
|
|||
|
||||
// TODO: handle errors, retry logic, and self-healing
|
||||
|
||||
return outputSchema.parse(output)
|
||||
return {
|
||||
result: outputSchema.parse(output),
|
||||
metadata: {}
|
||||
}
|
||||
} else {
|
||||
return output
|
||||
return {
|
||||
result: output,
|
||||
metadata: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
25
src/task.ts
25
src/task.ts
|
@ -6,8 +6,12 @@ import { Agentic } from './agentic'
|
|||
/**
|
||||
* A `Task` is a typed, async function call that may be non-deterministic.
|
||||
*
|
||||
* Invoking a task is equivalent to sampling from a probability distribution.
|
||||
*
|
||||
* Examples of tasks include:
|
||||
* - LLM calls
|
||||
* - Chain of LLM calls
|
||||
* - Retrieval task
|
||||
* - API calls
|
||||
* - Native function calls
|
||||
* - Invoking sub-agents
|
||||
|
@ -17,8 +21,9 @@ export abstract class BaseTask<
|
|||
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny
|
||||
> {
|
||||
protected _agentic: Agentic
|
||||
protected _timeoutMs: number | undefined
|
||||
protected _retryConfig: types.RetryConfig | undefined
|
||||
|
||||
protected _timeoutMs?: number
|
||||
protected _retryConfig?: types.RetryConfig
|
||||
|
||||
constructor(options: types.BaseTaskOptions) {
|
||||
this._agentic = options.agentic
|
||||
|
@ -45,9 +50,21 @@ export abstract class BaseTask<
|
|||
return this
|
||||
}
|
||||
|
||||
public abstract call(
|
||||
public async call(
|
||||
input?: types.ParsedData<TInput>
|
||||
): Promise<types.ParsedData<TOutput>>
|
||||
): Promise<types.ParsedData<TOutput>> {
|
||||
return this._call(input).then((response) => response.result)
|
||||
}
|
||||
|
||||
public async callWithMetadata(
|
||||
input?: types.ParsedData<TInput>
|
||||
): Promise<types.TaskResponse<TOutput>> {
|
||||
return this._call(input)
|
||||
}
|
||||
|
||||
protected abstract _call(
|
||||
input?: types.ParsedData<TInput>
|
||||
): Promise<types.TaskResponse<TOutput>>
|
||||
|
||||
// TODO
|
||||
// abstract stream({
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { z } from 'zod'
|
||||
|
||||
import * as types from '../types'
|
||||
import { Agentic } from '../agentic'
|
||||
import { MetaphorClient } from '../services/metaphor'
|
||||
import { BaseTask } from '../task'
|
||||
|
@ -57,15 +58,20 @@ export class MetaphorSearchTool extends BaseTask<
|
|||
return MetaphorSearchToolOutputSchema
|
||||
}
|
||||
|
||||
override async call(
|
||||
protected override async _call(
|
||||
input: MetaphorSearchToolInput
|
||||
): Promise<MetaphorSearchToolOutput> {
|
||||
): Promise<types.TaskResponse<typeof MetaphorSearchToolOutputSchema>> {
|
||||
// TODO: handle errors gracefully
|
||||
input = this.inputSchema.parse(input)
|
||||
|
||||
return this._metaphorClient.search({
|
||||
const result = await this._metaphorClient.search({
|
||||
query: input.query,
|
||||
numResults: input.numResults
|
||||
})
|
||||
|
||||
return {
|
||||
result,
|
||||
metadata: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,4 +105,12 @@ export interface RetryConfig {
|
|||
strategy: string
|
||||
}
|
||||
|
||||
export interface TaskResponse<
|
||||
TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
|
||||
TMetadata extends Record<string, any> = Record<string, any>
|
||||
> {
|
||||
result: ParsedData<TOutput>
|
||||
metadata: TMetadata
|
||||
}
|
||||
|
||||
// export type ProgressFunction = (partialResponse: ChatMessage) => void
|
||||
|
|
Ładowanie…
Reference in New Issue