Skip to content

Commit 80d3005

Browse files
author
imi
committed
x next files
1 parent 9de906d commit 80d3005

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

libs/XAI_next_1M1.jl

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
@api const XAI_next = """
2-
this knowledge connects to X AI. `files` in `next(input::String; files=[])::String` is currently unused (not yet implemented).
2+
this knowledge connects to X AI. `files` in `next(input::String; files=[])::String` now supports image file paths (jpg/png) by encoding them as base64 data URLs for vision capabilities.
33
"""
44

55
import Pkg
6-
Pkg.add(["HTTP", "JSON"])
7-
using HTTP, JSON
6+
Pkg.add(["HTTP", "JSON", "Base64"])
7+
using HTTP, JSON, Base64
88

99
@api MAX_OUTPUT_TOKENS = 10000
1010

11-
function callXAIAPI(api_key::String, system_prompt::String, user_prompt::String; model::String="grok-4", max_tokens::Int)::String
11+
function callXAIAPI(api_key::String, system_prompt::String, messages::Vector{Dict{String, Any}}; model::String="grok-4", max_tokens::Int)::String
1212
url = "https://api.x.ai/v1/chat/completions"
1313

1414
headers = [
@@ -18,23 +18,46 @@ function callXAIAPI(api_key::String, system_prompt::String, user_prompt::String;
1818

1919
body = Dict(
2020
"model" => model,
21-
"messages" => [
22-
Dict("role" => "system", "content" => system_prompt),
23-
Dict("role" => "user", "content" => user_prompt)
24-
],
21+
"messages" => messages,
2522
"max_tokens" => max_tokens,
2623
"temperature" => 0.2,
2724
"top_p" => 0.5,
28-
# "frequency_penalty" => 0.2,
29-
# "presence_penalty" => 0.2
3025
)
31-
@info length(user_prompt)
26+
27+
@debug length(JSON.json(body))
28+
3229
response = HTTP.post(url, headers, JSON.json(body))
3330
result = JSON.parse(String(response.body))
3431
result["choices"][1]["message"]["content"]
3532
end
3633

37-
@api function next(input::String; files=[])::String
34+
@api function next(input::String; files::Vector{String}=String[])::String
3835
global YOUR_PURPOSE, MAX_OUTPUT_TOKENS
39-
callXAIAPI(ENV["X_AI_API_KEY"], YOUR_PURPOSE, input, maxTokens=MAX_OUTPUT_TOKENS)
36+
37+
messages = [
38+
Dict("role" => "system", "content" => YOUR_PURPOSE)
39+
]
40+
41+
user_content = Any[Dict("type" => "text", "text" => input)]
42+
43+
for file in files
44+
if !isfile(file)
45+
error("File not found: $file")
46+
end
47+
ext = lowercase(splitext(file)[2])
48+
if ext [".jpg", ".jpeg", ".png"]
49+
error("Unsupported file type: $ext. Only jpg/jpeg/png supported.")
50+
end
51+
mime = ext == ".png" ? "image/png" : "image/jpeg"
52+
data = read(file)
53+
base64_data = base64encode(data)
54+
push!(user_content, Dict(
55+
"type" => "image_url",
56+
"image_url" => Dict("url" => "data:$mime;base64,$base64_data")
57+
))
58+
end
59+
60+
push!(messages, Dict("role" => "user", "content" => user_content))
61+
62+
callXAIAPI(ENV["X_AI_API_KEY"], YOUR_PURPOSE, messages; max_tokens=MAX_OUTPUT_TOKENS)
4063
end

src/core.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# + restrict write area: running user
2-
31
const YOUR_PURPOSE = "you are an a computer operating system"
42

53
abstract type IODevice end

0 commit comments

Comments
 (0)