Skip to content

Instantly share code, notes, and snippets.

@e2r2fx
Created October 22, 2024 16:21
Show Gist options
  • Select an option

  • Save e2r2fx/72116fdc3560912b70ee9457e30cf8f4 to your computer and use it in GitHub Desktop.

Select an option

Save e2r2fx/72116fdc3560912b70ee9457e30cf8f4 to your computer and use it in GitHub Desktop.
open ai image tool for codecompanion
local xml2lua = require("codecompanion.utils.xml.xml2lua")
local log = require("codecompanion.utils.log")
local function get_secret()
local cmd = os.getenv("HOME") .. "/scripts/get_secret.sh"
local handle = io.popen(cmd, "r")
if handle then
local result = handle:read("*a")
log:trace("Executed cmd: %s", cmd)
handle:close()
return result:gsub("%s+$", "")
else
return log:error("Error: Could not execute cmd: %s", cmd)
end
end
---@class CodeCompanion.Tool
return {
name = "img",
cmds = {
{
"curl",
"${url}",
"-H",
"Accept: application/json",
"-H",
"Content-Type: application/json",
"-H",
"Authorization: Bearer ${api_key}",
"-d",
'{ "model": "gpt-4o-mini", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "${query}" }, { "type": "image_url", "image_url": { "url": "${image_url}" } } ] } ], "max_tokens": 300 }',
},
},
schema = {
tool = {
_attr = { name = "img" },
action = {
url = "https://media.istockphoto.com/id/1944476521/photo/portrait-of-a-pretty-blue-merle-australian-shepherd-dog-looking-straigth-at-the-camera-with.jpg?s=1024x1024&w=is&k=20&c=cUz-C8K91qUiT5PXys3mu5cYGFLwRYShqg8jH0V89jQ=",
query = "Describe the image",
},
},
},
env = function(tool)
local api_key = get_secret()
return {
url = "https://api.openai.com/v1/chat/completions",
image_url = tool.action.url,
api_key = api_key,
query = tool.action.query,
}
end,
system_prompt = function(schema)
local xmlstring = xml2lua.toXml({ tools = { schema } })
local temp = string.format(
[[### You have gained access to a new tool!
Name: IMG (image anaylisis with vision ai)
Purpose: This gives you the ability to anaylise images
Why: Sometimes you may need to analyize images from urls to help you with your responses
Usage: To use this tool, you need to return an XML markdown code block (with backticks). Consider the following schema:
```xml
%s
```
You must:
- Only use the tool when you are provided with an image url in the context
- Be mindful that you may not be required to use the tool in all of your responses
- However if the user states you should use the image tool directly, you should use it without bothering the user
- Ensure the XML markdown code block is valid and follows the schema]],
xmlstring
)
return temp
end,
output_error_prompt = function(error)
if type(error) == "table" then
error = table.concat(error, "\n")
end
return string.format(
[[After the tool completed, there was an error:
```
%s
```
]],
error
)
end,
output_prompt = function(output)
if type(output) == "table" then
output = table.concat(output, "\n")
output = vim.json.decode(output)
output = output.choices[1].message.content
end
return string.format(
[[After viewing the image, this is what the tool returned:
### tool output
```
%s
```
]],
output
)
end,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment