Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
haouarihk authored Jan 12, 2024
1 parent 5718734 commit 5f89f10
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 23 deletions.
18 changes: 9 additions & 9 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"packageId": "Put_Package_Id_Here",
"name": "Name of your package",
"packageId": "vision",
"name": "Vision Package",
"version": "0.0.1",
"minTextGeneratorVersion": "0.1.0",
"description": "The description of the content of this package",
"author": "Your name",
"tags": "writing, brainstorming",
"authorUrl": "Support URL",
"repo": "username/repo"
}
"minTextGeneratorVersion": "0.6.0",
"description": "Contains Vision Templates and support for Vision for other templates (Scripts)",
"author": "Noureddine",
"tags": "OpenAI,markdown,gpt-4-vision,vision,images",
"authorUrl": "https://www.buymeacoffee.com/haouarine",
"repo": "text-gen/tg-vision"
}
81 changes: 81 additions & 0 deletions prompts/askGPT4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
promptId: askGPT4
name: 🗞️Ask GPT-4-Vision (only works with call from another template)
description: Using OpenAI's GPT-4-Vision, describe Image
author: Noureddine
tags:
- OpenAI
- markdown
- gpt-4-vision
- vision
- images
version: 0.0.1
disableProvider: true
viewTypes:
- none
---


```handlebars
You can structure your code here and then use the input or output template to retrieve("get" helper) the processed data, enhancing readability.
```
***
This input template is currently disabled due to the 'disabledProvider' setting being set to true.

If you wish to utilize this template with a provider, such as Chatbot or another service, please follow these steps:
- Enable the provider by setting 'disabledProvider' to false.
- Cut and paste everything from the output template into this section.
- Replace the content in the output template with '{{output}}'.
- Remember to delete this instruction text.
***
{{#script}}
```js
const OPENAI_API_KEY = plugin.getApiKeys().openAIChat;

async function askGPT4WithImage(promptText, base64Image, options = {}) {
  try {
    const requestOptions = {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${OPENAI_API_KEY}`,
        'Content-Type': 'application/json'
      },

      body: JSON.stringify({
        model: 'gpt-4-vision-preview',
        messages: [
          {
            role: 'user',
            content: [
              { type: 'text', text: promptText },
              { type: 'image_url', image_url: base64Image }
            ]
          }
        ],
        max_tokens: 300,
        ...options
      })
    };



    const response = await fetch('https://api.openai.com/v1/chat/completions', requestOptions);

    if (!response.ok) {
      throw new Error(`OpenAI API request failed with status ${response.status}`);
    }

    const chatCompletionData = await response.json();

    console.log(chatCompletionData);
    return chatCompletionData.choices[0].message.content;

  } catch (error) {
    console.error('Error in asking GPT-4 with an image:', error);
    throw error;
  }
}

return await askGPT4WithImage(this.prompt, this.base64Image);
```
{{/script}}
106 changes: 106 additions & 0 deletions prompts/describe_Images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
promptId: describe_Images
name: 🗞️GPT-4-Vision describe Image
description: Using OpenAI's GPT-4-Vision, describe Image
author: Noureddine
tags:
- OpenAI
- markdown
- gpt-4-vision
- vision
- images
version: 0.0.1
disableProvider: true
---
```handlebars
You can structure your code here and then use the input or output template to retrieve("get" helper) the processed data, enhancing readability.
```
***
This input template is currently disabled due to the 'disabledProvider' setting being set to true.

If you wish to utilize this template with a provider, such as Chatbot or another service, please follow these steps:
- Enable the provider by setting 'disabledProvider' to false.
- Cut and paste everything from the output template into this section.
- Replace the content in the output template with '{{output}}'.
- Remember to delete this instruction text.
***
{{#script}}
```js
async function imageFileToBase64(vault, imageFilePath) {
  const imageFile = vault.getAbstractFileByPath(imageFilePath);  
  const arrayBuffer = await vault.readBinary(imageFile);
  const base64String = arrayBufferToBase64(arrayBuffer);
  return `data:image/jpeg;base64,${base64String}`;
}



function arrayBufferToBase64(buffer) {
  let binary = '';

  const bytes = new Uint8Array(buffer);

  const len = bytes.byteLength;

  for (let i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i]);
  }

  return window.btoa(binary);
}

async function resolveFullPathInVault(filename) {
  // Get all the markdown files in the vault.
  const files = app.vault.getFiles();

  // Find the file with the specified filename.
  const file = files.find(file => file.name === filename);

  // If the file is found, return its path.
  return file ? file.path : null;
}

const descriptions = {};
async function processImagesAndInsertContent(prompt,matches) {
  for (const match of matches) {
  const filename = match.substring(2,match.length-2)
  try{
const fullPath = await resolveFullPathInVault(filename);
console.log({ fullPath });

if (!fullPath) return console.error("Image file not found in vault: " + filename);

// Get the Base64 string for the image
const base64Image = await imageFileToBase64(app.vault, fullPath);

// Use the Base64 string as an image URL to call the API
const content = await run("askGPT4", {prompt, base64Image});

// Insert the name of the image followed by the content
descriptions[filename] = content;
} catch(err){
notice(err?.message || JSON.stringify(err));
}
  }
}

const selection = this.tg_selection;

const regex = /\[\[(.*?)(\.png|\.jpeg|\.jpg|\.webp)]]/gi;

// Use match to find all occurrences
const matches = selection.match(regex);

const PROMPT_TEXT = "What’s in this image?";

if (matches && matches.length)
  await processImagesAndInsertContent(PROMPT_TEXT, matches);
else
  console.error("No image files found (.png, .jpeg, .jpg, .webp).");

return Object.entries(descriptions).map(([filename, description])=>
`**${filename}**:
${description}`
)
```
{{/script}}
14 changes: 0 additions & 14 deletions prompts/getExample.md

This file was deleted.

0 comments on commit 5f89f10

Please sign in to comment.