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

Update defImages function to support multiple file types and handle Blob conversion #705

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions docs/src/content/docs/reference/scripts/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@
```

Read more about [OpenAI Vision](https://platform.openai.com/docs/guides/vision/limitations).

## URLs

Public URLs (that do not require authentication) will be passed directly to OpenAI.

```js
defImages(
"https://github.com/microsoft/genaiscript/blob/main/docs/public/images/logo.png?raw=true"
)
```

Local files are loaded and encoded as a data uri.

## Buffer, Blob

The `defImage` function also supports [Buffer](https://nodejs.org/api/buffer.html)

Check failure on line 33 in docs/src/content/docs/reference/scripts/images.md

View workflow job for this annotation

GitHub Actions / build

Incorrect function name 'defImage', should be 'defImages' as used consistently in the document.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name defImage should be defImages to match the function used in the other documentation file.

generated by pr-docs-review-commit typo

and [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob).


This example takes a screenshot of bing.com and adds it to the images.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in the word "bing.com", should be "Bing.com" as it is a proper noun.

generated by pr-docs-review-commit typo


```js
const page = await host.browse("https://bing.com")
const screenshot = await page.screenshot() // returns a node.js Buffer

Check failure on line 41 in docs/src/content/docs/reference/scripts/images.md

View workflow job for this annotation

GitHub Actions / build

The function 'page.screenshot' is used but not defined in the documentation. There should be a definition or reference for it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function 'page.screenshot' is used but not defined in the documentation. There should be a definition or reference for it.

generated by pr-docs-review-commit missing_function_definition

defImages(screenshot)

Check failure on line 42 in docs/src/content/docs/reference/scripts/images.md

View workflow job for this annotation

GitHub Actions / build

The function 'host.browse' is used but not defined in the documentation. There should be a definition or reference for it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function 'host.browse' is used but not defined in the documentation. There should be a definition or reference for it.

generated by pr-docs-review-commit missing_function_definition

```
6 changes: 4 additions & 2 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,51 @@
) => {
appendChild(node, createSchemaNode(name, schema, defOptions))

return name
}

const defImages = (files: StringLike, defOptions?: DefImagesOptions) => {
const defImages = (
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,
defOptions?: DefImagesOptions
) => {
const { detail } = defOptions || {}
if (Array.isArray(files))
files.forEach((file) => defImages(file, defOptions))
else if (typeof files === "string")
appendChild(node, createImageNode({ url: files, detail }))
else if (files instanceof Buffer) {
const buffer: Buffer = files
appendChild(
node,
createImageNode(
(async () => {
const mime = await fileTypeFromBuffer(buffer)
const b64 = await buffer.toString("base64")
const url = `data:${mime.mime};base64,${b64}`
return {
url,
detail,
}
})()
)
)
} else if (files instanceof Blob) {
const blob: Blob = files
appendChild(
node,
createImageNode(
(async () => {
const buffer = Buffer.from(await blob.arrayBuffer())
const mime = await fileTypeFromBuffer(buffer)
const b64 = await buffer.toString("base64")
const url = `data:${mime.mime};base64,${b64}`
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
return {
url,
detail,
}
})()
)
)

Check failure on line 282 in packages/core/src/runpromptcontext.ts

View workflow job for this annotation

GitHub Actions / build

You are creating an async function inside a loop. This can lead to performance issues as each async function returns a promise which is then awaited. Consider refactoring this to use Promise.all to handle all promises at once.

Check failure on line 282 in packages/core/src/runpromptcontext.ts

View workflow job for this annotation

GitHub Actions / build

You are using async functions but not handling potential errors with a try/catch block. This can lead to unhandled promise rejections which can crash the application.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function defImages is missing a return type. It's a good practice to always specify a return type for functions to improve code readability and prevent potential bugs.

generated by pr-review-commit missing_return_type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are creating an async function but not awaiting it, which can lead to unexpected behavior. Consider adding an await keyword or returning the promise.

generated by pr-review-commit async_without_await

} else {
const file: WorkspaceFile = files
appendChild(
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type OptionsOrString<TOptions extends string> = (string & {}) | TOptions

type ElementOrArray<T> = T | T[]

interface PromptGenerationConsole {
log(...data: any[]): void
warn(...data: any[]): void
Expand Down Expand Up @@ -1537,7 +1539,7 @@
options?: DefSchemaOptions
): string
defImages(
files: StringLike | Buffer | Blob,
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,

Check failure on line 1542 in packages/core/src/types/prompt_template.d.ts

View workflow job for this annotation

GitHub Actions / build

The change in the type of the 'files' parameter from 'StringLike | Buffer | Blob' to 'ElementOrArray<string | WorkspaceFile | Buffer | Blob>' in the 'defImages' function is a breaking change. This can cause issues in the existing code where this function is used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in the type of the 'files' parameter from 'StringLike | Buffer | Blob' to 'ElementOrArray<string | WorkspaceFile | Buffer | Blob>' in the 'defImages' function is a breaking change. This can cause issues in the existing code where this function is used.

generated by pr-review-commit breaking_change

options?: DefImagesOptions
): void
defTool(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/prompt_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ declare function defSchema(
* @param options
*/
declare function defImages(
files: StringLike | Buffer | Blob,
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,
options?: DefImagesOptions
): void

Expand Down
35 changes: 35 additions & 0 deletions packages/sample/genaisrc/azure-blog-storage.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BlobServiceClient } from "@azure/storage-blob"
import { DefaultAzureCredential } from "@azure/identity"

script({
parameters: {
account: {
description: "Azure Storage Account Name",
default: "myaccount",
type: "string",
},
container: {
description: "Azure Storage Container Name",
default: "mycontainer",
type: "string",
},
},
})

const { account, container } = env.vars
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
new DefaultAzureCredential()
)
const containerClient = blobServiceClient.getContainerClient(container)
for await (const blob of containerClient.listBlobsFlat()) {
const blockBlobClient = containerClient.getBlockBlobClient(blob.name)
const downloadBlockBlobResponse = await blockBlobClient.download(0)
const body = await downloadBlockBlobResponse.readableStreamBody
const buffer = await body.read()
const res = await runPrompt((_) => {
_.defImages(buffer)
_.$`Describe the image.`
})
// do something with res?
}
6 changes: 4 additions & 2 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"@agentic/calculator": "^7.0.0",
"@agentic/core": "^7.0.0",
"@agentic/weather": "^7.0.0",
"@azure/identity": "^4.4.1",
"@azure/storage-blob": "^12.24.0",
"@tidyjs/tidy": "^2.5.2",
"@xenova/transformers": "^2.17.2",
"vectorstore": "^0.0.4",
Expand Down
6 changes: 4 additions & 2 deletions packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/errors/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading