fix: make image processing work in github copilot provider (#5687)
This commit is contained in:
@@ -132,6 +132,29 @@ pub struct GithubCopilotProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GithubCopilotProvider {
|
impl GithubCopilotProvider {
|
||||||
|
fn payload_contains_image(payload: &Value) -> bool {
|
||||||
|
payload
|
||||||
|
.get("messages")
|
||||||
|
.and_then(|m| m.as_array())
|
||||||
|
.is_some_and(|messages| {
|
||||||
|
messages.iter().any(|msg| {
|
||||||
|
msg.get("content").is_some_and(|content| {
|
||||||
|
content
|
||||||
|
.as_array()
|
||||||
|
.map(|arr| arr.iter().collect::<Vec<_>>())
|
||||||
|
.unwrap_or_else(|| vec![content])
|
||||||
|
.iter()
|
||||||
|
.any(|item| {
|
||||||
|
matches!(
|
||||||
|
item.get("type").and_then(|v| v.as_str()),
|
||||||
|
Some("image_url") | Some("image")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn from_env(model: ModelConfig) -> Result<Self> {
|
pub async fn from_env(model: ModelConfig) -> Result<Self> {
|
||||||
let client = Client::builder()
|
let client = Client::builder()
|
||||||
.timeout(Duration::from_secs(600))
|
.timeout(Duration::from_secs(600))
|
||||||
@@ -164,14 +187,21 @@ impl GithubCopilotProvider {
|
|||||||
let (endpoint, token) = self.get_api_info().await?;
|
let (endpoint, token) = self.get_api_info().await?;
|
||||||
let url = url::Url::parse(&format!("{}/chat/completions", endpoint))
|
let url = url::Url::parse(&format!("{}/chat/completions", endpoint))
|
||||||
.map_err(|e| ProviderError::RequestFailed(format!("Invalid base URL: {e}")))?;
|
.map_err(|e| ProviderError::RequestFailed(format!("Invalid base URL: {e}")))?;
|
||||||
let response = self
|
|
||||||
|
let headers = self.get_github_headers();
|
||||||
|
|
||||||
|
let mut request = self
|
||||||
.client
|
.client
|
||||||
.post(url)
|
.post(url)
|
||||||
.headers(self.get_github_headers())
|
.headers(headers)
|
||||||
.header("Authorization", format!("Bearer {}", token))
|
.header("Authorization", format!("Bearer {}", token));
|
||||||
.json(payload)
|
|
||||||
.send()
|
if Self::payload_contains_image(payload) {
|
||||||
.await?;
|
request = request.header("Copilot-Vision-Request", "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
let response = request.json(payload).send().await?;
|
||||||
|
|
||||||
if stream_only_model {
|
if stream_only_model {
|
||||||
let mut collector = OAIStreamCollector::new();
|
let mut collector = OAIStreamCollector::new();
|
||||||
let mut stream = response.bytes_stream();
|
let mut stream = response.bytes_stream();
|
||||||
|
|||||||
Reference in New Issue
Block a user