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

Conversation

pelikhan
Copy link
Member

@pelikhan pelikhan commented Sep 11, 2024

This pull request updates the defImages function to support multiple file types and handle Blob conversion. The files parameter now accepts an array of strings, WorkspaceFiles, Buffers, or Blobs. This allows for more flexibility when passing files to the function.

  • The defImages function in runpromptcontext.ts has been updated to handle Blob instances 👏. Now, when a Blob instance is provided as an input file, it's converted into an 'image' node and appended to the parent node.

  • defImages function parameters across various files have been made more flexible by introducing a new type, ElementOrArray. This means defImages can now handle both individual elements and arrays of elements of the types string, WorkspaceFile, Buffer, or Blob 🔄.

  • These changes are reflected in the relevant type declaration files i.e. prompt_template.d.ts and prompt_type.d.ts. More specifically, the defImages function signatures in these files replace StringLike | Buffer | Blob with ElementOrArray<string | WorkspaceFile | Buffer | Blob>.

  • This shows clarity in the public API regarding what types of inputs defImages can accept, enhancing user understanding and control 🎮. The change increases the flexibility of data inputs making the system more robust and user-friendly.

  • Overall, fantastic job on improving flexibility, user compatibility, and making operations more efficient. Keep up the good work! 🚀🙌

generated by pr-describe

Copy link

The changes in the pull request look good to me. Here is a brief summary of the changes:

  1. The method defImages in runpromptcontext.ts now accepts an additional server-side file type, Blob. The Blob files are converted to base64 and used to generate an image node.

  2. The ElementOrArray<T> type has been introduced which allows passing an element or an array of elements of type T.

  3. The input parameter type of the method defImages in prompt_template.d.ts and prompt_type.d.ts has been changed to ElementOrArray<string | WorkspaceFile | Buffer | Blob>, which introduces the changes made in runpromptcontext.ts at the type level.

The changes seem to be well-implemented and no functional issues are visible.

LGTM 🚀

generated by pr-review

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

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


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

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

@@ -1537,7 +1539,7 @@ interface ChatGenerationContext extends ChatTurnGenerationContext {
options?: DefSchemaOptions
): string
defImages(
files: StringLike | Buffer | Blob,
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,

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

}
})()
)
)

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

import { Steps } from "@astrojs/starlight/components"
import source from "../../../../../packages/sample/genaisrc/azure-blobs.genai.mts?raw"

It is possible to use the Azure Node.JS SDK to download images from Azure Blog Storage

Choose a reason for hiding this comment

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

There is a typo in "Blog" which should be "Blob".

generated by pr-docs-review-commit typo

import source from "../../../../../packages/sample/genaisrc/azure-blobs.genai.mts?raw"

It is possible to use the Azure Node.JS SDK to download images from Azure Blog Storage
and use them in the prompt. The `defImages` function support the node.js [Buffer] type.

Choose a reason for hiding this comment

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

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

generated by pr-docs-review-commit typo


Open a connection to the Azure Blob Storage and get a client to the container.
We deconstruct the `account` and `container` from the `env.vars` object
so that they can be set through the [cli](/genaiscript/reference/cli).

Choose a reason for hiding this comment

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

The link provided for [cli] is incorrect or broken, it should be a valid URL or path to the CLI reference.

generated by pr-docs-review-commit broken_link

If you do not have a specific blob in mind, you can iterate through the blobs,
and download them into a buffer (`buf`).

```ts "image"

Choose a reason for hiding this comment

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

The code fence "image" is incorrect or unnecessary, it should be removed.

generated by pr-docs-review-commit incorrect_code_fence

However since images can be "heavy", you will most likely have to use
[inline prompts](/genaiscript/reference/prompts/inline-prompts) to split into smaller queries. (Note the use of `_.`)

```ts 'await runPrompt(_ =>' '_.'

Choose a reason for hiding this comment

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

There is a syntax error in the code snippet, the single quotes and backticks are not correctly paired.

generated by pr-docs-review-commit syntax_error

However since images can be "heavy", you will most likely have to use
[inline prompts](/genaiscript/reference/prompts/inline-prompts) to split into smaller queries. (Note the use of `_.`)

```ts 'await runPrompt(_ =>' '_.'

Choose a reason for hiding this comment

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

The code fence 'await runPrompt(_ =>' ' _.' is incorrect or unnecessary, it should be removed.

generated by pr-docs-review-commit incorrect_code_fence

defImages(image, { detail: "low" })
```

However since images can be "heavy", you will most likely have to use

Choose a reason for hiding this comment

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

The sentence "However since images can be 'heavy', you will most likely have to use" could be clarified to specify that "heavy" refers to large file sizes which may require more processing time or resources.

generated by pr-docs-review-commit clarity


## Full source

<Code code={source} wrap={true} lang="js" title="azure-blobs.genai.mts" />

Choose a reason for hiding this comment

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

The variable source is used but not defined in the document, which might confuse readers. It should be clearly defined or explained.

generated by pr-docs-review-commit missing_reference


## Buffer, Blob

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

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

…documentation details across various guides.
If you do not have a specific blob in mind, you can iterate through the blobs,
and download them into a buffer (`buf`).

```ts "image"

Choose a reason for hiding this comment

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

The code comment "image" seems to be misplaced or incorrect.

generated by pr-docs-review-commit incorrect_code_comment

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

}
})()
)
)

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

@pelikhan pelikhan merged commit 9786584 into main Sep 11, 2024
11 checks passed
@pelikhan pelikhan deleted the defimages-many branch September 11, 2024 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant