Skip to content

Commit

Permalink
attempt to fix vision, no success. i love obscure error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckyBlender committed Nov 27, 2024
1 parent 1e8cf48 commit 83f64bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
71 changes: 35 additions & 36 deletions src/apis/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,69 +127,70 @@ impl OpenAIClient {
let api_key = Self::get_api_key(&provider);
let system_prompt = Self::get_system_prompt(&model);

let mut messages = vec![];

// Add system prompt
if !system_prompt.is_empty() {
messages.push(json!({
"role": "system",
"content": system_prompt
}));
}

// Construct user message content
let mut user_content = vec![json!({
"type": "text",
"text": prompt
})];

// Add image if provided
if let Some(image) = base64_img {
user_content.push(json!({
"type": "image_url",
"image_url": {
"url": format!("data:image/jpeg;base64,{}", image)
let user_content = if let Some(image) = base64_img {
json!([
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": format!("data:image/jpeg;base64,{}", image) // telegram photos are always jpeg
}
}
}));
}
])
} else {
json!([{
"type": "text",
"text": prompt
}])
};

// Add user message
messages.push(json!({
"role": "user",
"content": user_content
}));
// Add system prompt and user message
let messages = vec![
json!({
"role": "system",
"content": system_prompt
}),
json!({
"role": "user",
"content": user_content
}),
];

let additional_headers = Self::get_additional_headers(&provider);
let temperature = Self::get_temperature(&model);

debug!("headers: {:?}", additional_headers);

let json_request = &json!({
let json_request = json!({
"model": model_str,
"messages": messages,
"max_tokens": 512,
"temperature": temperature,
"provider": {
"order": [
"SambaNova" // always prioritize SambaNova, if available
]
"order": ["SambaNova"]
}
});

// pure json
debug!("json_request: {}", json_request.to_string());

let response = self
.client
.post(format!("{provider_base_url}/chat/completions"))
.bearer_auth(api_key)
.headers(additional_headers)
.json(json_request)
.json(&json_request)
.send()
.await?;

let status = response.status();

let json_response: Value = response.json().await?;

// Object {"error": Object {"code": Number(429), "message": String("{\n \"error\": {\n \"message\": \"You have been rate limited for model meta-llama/Llama-Vision-Free. Your rate limit is 5 queries per minute. Please navigate to https://api.together.xyz/settings/billing to upgrade your plan and see your limit.\",\n \"type\": \"model_rate_limit\",\n \"param\": null,\n \"code\": null\n }\n}")}}
let ratelimited = status.as_u16() == 429
|| json_response
.get("error")
Expand All @@ -198,8 +199,6 @@ impl OpenAIClient {
.map(|code| u16::try_from(code).unwrap())
== Some(429);

// 502 unexpected error
// DEBUG code: 200 OK, response: Object {"choices": Array [Object {"error": Object {"code": Number(502), "message": String("Upstream error from SambaNova: unexpected_error"), "metadata": Object {"raw": Object {"code": Null, "message": String("unexpected_error"), "param": Null, "type": String("unexpected_error")}}}, "index": Number(0), "logprobs": Null, "message": Object {"content": String(""), "refusal": String(""), "role": String("assistant")}}], "created": Number(1730789139), "id": String("gen-1730789139-aEiqaOcLEE2gvtzeegwH"), "model": String("meta-llama/llama-3.2-90b-vision-instruct:free"), "object": String("chat.completion"), "provider": String("SambaNova"), "system_fingerprint": String("fastcoe"), "usage": Object {"completion_tokens": Number(0), "prompt_tokens": Number(1486), "total_tokens": Number(1486)}}
let unexpected_error = status.as_u16() == 502
|| json_response
.get("choices")
Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub async fn download_and_encode_image(bot: &Bot, media: &Media) -> anyhow::Resu
Media::Sticker(sticker) => &sticker.file.id,
};


let file = bot.get_file(file_id).await?;
bot.download_file(&file.path, &mut buf).await?;

Expand Down

0 comments on commit 83f64bc

Please sign in to comment.