The AI Chat Completion API processes text and image inputs to generate conversational responses. It supports various configurations to customize response behavior and manage input content.
POST/v1/chat/completions{"model":"cogvlm-chat-17b","messages":[{"role":"user","content":[{"type":"text","text":"Describe this image"},{"type":"image_url","image_url":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD..."}]}],"temperature":0.8,"top_p":0.9,"max_tokens":1024}
importrequestsimportbase64fromPILimportImagefromioimportBytesIO# Convert image to Base64defimage_to_base64(image_path):withImage.open(image_path)asimage:buffered=BytesIO()image.save(buffered,format="JPEG")img_str=base64.b64encode(buffered.getvalue()).decode("utf-8")returnimg_str# Replace 'image.jpg' with the path to your imagebase64_image=image_to_base64("your_image.jpg")text_data={"type":"text","text":"Describe what is in the image"}image_data={"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{base64_image}"},}# Construct the request datarequest_data={"model":"cogvlm-chat-17b","messages":[{"role":"user","content":[text_data,image_data]}],"temperature":0.8,"top_p":0.9,"max_tokens":1024,}# Specify the URL of your FastAPI applicationurl="https://api.swarms.world/v1/chat/completions"# Send the requestresponse=requests.post(url,json=request_data)# Print the response from the serverprint(response.text)
constfs=require('fs');consthttps=require('https');constsharp=require('sharp');// Convert image to Base64asyncfunctionimageToBase64(imagePath){try{constimageBuffer=awaitsharp(imagePath).jpeg().toBuffer();returnimageBuffer.toString('base64');}catch(error){console.error('Error converting image to Base64:',error);}}// Main function to execute the workflowasyncfunctionmain(){constbase64Image=awaitimageToBase64("your_image.jpg");consttextData={type:"text",text:"Describe what is in the image"};constimageData={type:"image_url",image_url:{url:`data:image/jpeg;base64,${base64Image}`},};// Construct the request dataconstrequestData=JSON.stringify({model:"cogvlm-chat-17b",messages:[{role:"user",content:[textData,imageData]}],temperature:0.8,top_p:0.9,max_tokens:1024,});constoptions={hostname:'api.swarms.world',path:'/v1/chat/completions',method:'POST',headers:{'Content-Type':'application/json','Content-Length':requestData.length,},};constreq=https.request(options,(res)=>{letresponseBody='';res.on('data',(chunk)=>{responseBody+=chunk;});res.on('end',()=>{console.log('Response:',responseBody);});});req.on('error',(error)=>{console.error(error);});req.write(requestData);req.end();}main();
packagemainimport("bytes""encoding/base64""encoding/json""fmt""image""image/jpeg"_"image/png"// Register PNG format"io""net/http""os")// imageToBase64 converts an image to a Base64-encoded string.funcimageToBase64(imagePathstring)(string,error){file,err:=os.Open(imagePath)iferr!=nil{return"",err}deferfile.Close()img,_,err:=image.Decode(file)iferr!=nil{return"",err}buf:=new(bytes.Buffer)err=jpeg.Encode(buf,img,nil)iferr!=nil{return"",err}returnbase64.StdEncoding.EncodeToString(buf.Bytes()),nil}// main is the entry point of the program.funcmain(){base64Image,err:=imageToBase64("your_image.jpg")iferr!=nil{fmt.Println("Error converting image to Base64:",err)return}requestData:=map[string]interface{}{"model":"cogvlm-chat-17b","messages":[]map[string]interface{}{{"role":"user","content":[]map[string]string{{"type":"text","text":"Describe what is in the image"},{"type":"image_url","image_url":{"url":fmt.Sprintf("data:image/jpeg;base64,%s",base64Image)}}},},},"temperature":0.8,"top_p":0.9,"max_tokens":1024,}requestBody,err:=json.Marshal(requestData)iferr!=nil{fmt.Println("Error marshaling request data:",err)return}url:="https://api.swarms.world/v1/chat/completions"request,err:=http.NewRequest("POST",url,bytes.NewBuffer(requestBody))iferr!=nil{fmt.Println("Error creating request:",err)return}request.Header.Set("Content-Type","application/json")client:=&http.Client{}response,err:=client.Do(request)iferr!=nil{fmt.Println("Error sending request:",err)return}deferresponse.Body.Close()responseBody,err:=io.ReadAll(response.Body)iferr!=nil{fmt.Println("Error reading response body:",err)return}fmt.Println("Response:",string(responseBody))}
This API reference provides the necessary details to understand and interact with the AI Chat Completion API. By following the outlined request and response formats, users can integrate this API into their applications to generate dynamic and contextually relevant conversational responses.