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

fix: vision tool instructions #62

Merged
merged 10 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions example/src/library_photo_to_website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const outputPath = path.join(workingDir, 'library.html')
const bookLibraryWorkflow = workflow({
members: [librarian, webmaster],
description: `
Analyze the photo of the library and list all the books in the library.
Generate a website that lists all the books in the library.
The photo of books in the library is in the "${imagePath}" file.

Expand Down
23 changes: 16 additions & 7 deletions packages/tools/src/vision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const encodeImage = async (imagePath: string): Promise<string> => {

async function callOpenAI(
provider: Provider,
analysis: string,
prompt: string,
image_url: string,
detail: 'low' | 'high'
) {
Expand All @@ -24,7 +24,7 @@ async function callOpenAI(
content: [
{
type: 'text',
text: `${analysis}. Use your built-in OCR capabilities.`,
text: `${prompt}. Use your built-in OCR capabilities.`,
},
{ type: 'image_url', image_url: { url: image_url, detail } },
],
Expand Down Expand Up @@ -57,12 +57,21 @@ async function callOpenAI(
}

export const visionTool = tool({
description: 'Tool for analyzing and OCR the pictures',
description: 'LLM AI Tool for analyzing and OCR the pictures',
parameters: z.object({
imagePathUrl: z.string().describe('Absolute path to image on disk or URL'),
analysis: z.string().describe(s`
outputFormat: z
.enum(['text', 'json'])
.default('text')
.describe(
'Output format of the data extracted from image - for example attributes you like to extract from the objects on image, JSON format for the document to OCR to etc'
),
prompt: z.string().describe(s`
Description of what to analyze and extract from the image, such as
text content, layout, font styles, and any specific data fields.'
text content, layout, font styles, and any specific data fields.
To use the vision tool properly provide it with the 'prompt' for a LLM multimodal model
pkarw marked this conversation as resolved.
Show resolved Hide resolved
which describes in details - which features to extract and analyze from the image image.
'
`),
detail: z
.enum(['low', 'high'])
Expand All @@ -71,10 +80,10 @@ export const visionTool = tool({
)
.default('high'),
}),
execute: async ({ imagePathUrl, detail, analysis }, { provider }) => {
execute: async ({ imagePathUrl, detail, prompt }, { provider }) => {
const imageUrl = imagePathUrl.startsWith('http')
? imagePathUrl
: await encodeImage(imagePathUrl)
return callOpenAI(provider, analysis, imageUrl, detail)
return callOpenAI(provider, prompt, imageUrl, detail)
},
})