Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add provider to each function #25

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/framework/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export async function executeTaskWithAgent(
zodFunction({
name,
parameters: tool.parameters,
function: tool.execute,
description: tool.description,
})
)
Expand Down Expand Up @@ -74,7 +73,9 @@ export async function executeTaskWithAgent(
throw new Error(`Unknown tool: ${toolCall.function.name}`)
}

const content = await tool.execute(toolCall.function.parsed_arguments)
const content = await tool.execute(toolCall.function.parsed_arguments, {
provider: agent.provider,
})
return {
role: 'tool' as const,
tool_call_id: toolCall.id,
Expand Down
7 changes: 6 additions & 1 deletion packages/framework/src/tool.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import z, { ZodTypeAny } from 'zod'

import { Provider } from './models/openai.js'

export type Tool<P extends ZodTypeAny = any> = {
description: string
parameters: P
execute: (parameters: z.infer<P>) => Promise<string>
execute: (parameters: z.infer<P>, context: { provider: Provider }) => Promise<string>
}

/**
* Helper function to infer the type of the parameters.
*/
export const tool = <P extends ZodTypeAny>(tool: Tool<P>): Tool<P> => tool
17 changes: 8 additions & 9 deletions packages/tools/src/vision/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Example borrowed from CrewAI.
*/
import { openai } from '@dead-simple-ai-agent/framework/models/openai'
import { Provider } from '@dead-simple-ai-agent/framework/models/openai'
import { tool } from '@dead-simple-ai-agent/framework/tool'
import * as fs from 'fs'
import { z } from 'zod'
Expand All @@ -10,11 +10,10 @@ const encodeImage = (imagePath: string): string => {
const imageBuffer = fs.readFileSync(imagePath)
return imageBuffer.toString('base64')
}
const visionProvider = openai()

const runLocalImages = async (imagePathUrl: string): Promise<string> => {
const runLocalImages = async (provider: Provider, imagePathUrl: string): Promise<string> => {
const base64Image = encodeImage(imagePathUrl)
const response = await visionProvider.completions({
const response = await provider.completions({
model: 'gpt-4o-mini',
messages: [
{
Expand All @@ -30,8 +29,8 @@ const runLocalImages = async (imagePathUrl: string): Promise<string> => {
return response.choices[0].message.content as string
}

const runWebHostedImages = async (imagePathUrl: string): Promise<string> => {
const response = await visionProvider.completions({
const runWebHostedImages = async (provider: Provider, imagePathUrl: string): Promise<string> => {
const response = await provider.completions({
model: 'gpt-4o-mini',
messages: [
{
Expand All @@ -53,15 +52,15 @@ export const visionTool = tool({
parameters: z.object({
imagePathUrl: z.string().describe('The image path or URL'),
}),
execute: ({ imagePathUrl }) => {
execute: ({ imagePathUrl }, { provider }) => {
if (!imagePathUrl) {
throw new Error('Image Path or URL is required.')
}

if (imagePathUrl.startsWith('http')) {
return runWebHostedImages(imagePathUrl)
return runWebHostedImages(provider, imagePathUrl)
} else {
return runLocalImages(imagePathUrl)
return runLocalImages(provider, imagePathUrl)
}
},
})