lanqian528 / chat2api

A service that can convert ChatGPT on the web to OpenAI API format.
MIT License
1.98k stars 390 forks source link

返回的图片url无法下载保存 #148

Open fouvy opened 2 weeks ago

fouvy commented 2 weeks ago

代码如下:

import requests

def chat_with_gpt(prompt,img_url):
    api_key = "your api key"
    model = "gpt-4o"
    url = "http://127.0.0.1:5005/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    data = {
        "model": model,
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url":img_url
                        }
                    }
                ]
            },
            {
                "role": "system",
                "content": [
                    {
                        "type": "text",
                        "text": prompt
                    }
                ]
            } 
        ]
    }

    response = requests.post(url, headers=headers, json=data)

    if response.status_code == 200:
        response_data = response.json()
        return response_data['choices'][0]['message']['content']

    else:
        return f"Error: {response.status_code}, {response.text}"

def test1():
    img_url="https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
    prompt="帮我画一张类似的2D图片"
    response = chat_with_gpt(prompt,img_url) 
    print(response)

if __name__=="__main__":
    test1()

返回的结果:

json
{
  "size": "1024x1024",
  "prompt": "A 2D illustration of a person sitting on the beach playing with a large dog. The person is sitting cross-legged, smiling and wearing a checkered shirt, while the dog, a large breed with a light-colored coat, sits facing the person, raising its paw to gently touch the person's hand. The beach scene has a peaceful, sunset atmosphere, with soft ocean waves and a golden glow in the background. The illustration should have a playful and warm feeling, capturing the bond between the person and the dog."
}
![image](https://files.oaiusercontent.com/file-TBq8puVoBqTltH0Bk47jI8tA?se=2024-09-29T10%3A28%3A37Z&sp=r&sv=2024-08-04&sr=b&rscc=max-age%3D604800%2C%20immutable%2C%20private&rscd=attachment%3B%20filename%3D98dd12de-9b04-4d09-b7ec-a3d7759c94e7.webp&sig=UKGxaKm9pN/ro8n8ks5ihU9OEzZTeYcPTnSHtBjBG04%3D)
Here is the 2D illustration based on your request! Let me know if you need any adjustments.

上面的image后面的url地址的图片如何下载?我直接把前面的https://files.oaiusercontent.com改成http://127.0.0.1:5005也无法下载。

fouvy commented 1 week ago

已经解决