import requests 
import json 

url = "http://localhost:11434/api/generate"

headers = {
    "Content-Type": "application/json"
}

data = {
    "model": "qwen2:0.5b",
    "prompt": "Why is the sky black? Answer in Chinese.",
    "stream": False
}

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

if response.status_code == 200:
    response_text = response.text
    data = json.loads(response_text)
    actual_response = data["response"]
    print(actual_response)
else:
    print("error:", response.status_code, response.text)


{

    "model""qwen2:0.5b",

    "created_at""2024-08-22T06:20:16.768091631Z",

    "response""天空为什么是黑色的?因为光线穿过大气层时,会受到不同的散射现象影响。具体来说,大气分子(如氮气、氧气等)和水蒸气阻止了阳光中的大部分波长光线,导致太阳光中只有红、橙、黄、绿、蓝五颜六色的光线被散射;而蓝色、紫色光线则被吸收并反射回去。结果就是天空呈现出黑色。",

    "done"true,

    "done_reason""stop",

    "context"[
        151644,
        872,
        198,
        10234,
        374,
        279,
        12884,
        3691,
        30,
        21806,
        304,
        8453,
        13,
        151645,
        198,
        151644,
        77091,
        198,
        101916,
        100678,
        20412,
        104723,
        9370,
        11319,
        99519,
        109587,
        109239,
        105797,
        99371,
        13343,
        3837,
        36993,
        100683,
        101970,
        99632,
        99759,
        102060,
        99564,
        1773,
        100398,
        99883,
        3837,
        105797,
        102388,
        9909,
        29524,
        109958,
        99180,
        5373,
        115893,
        49567,
        7552,
        33108,
        52510,
        101494,
        99180,
        107345,
        34187,
        104166,
        101047,
        101212,
        99804,
        45861,
        109587,
        3837,
        100673,
        101281,
        99225,
        15946,
        101043,
        99425,
        5373,
        107678,
        5373,
        99789,
        5373,
        99679,
        5373,
        100400,
        75108,
        100847,
        99566,
        38035,
        9370,
        109587,
        99250,
        99632,
        99759,
        24968,
        68536,
        105681,
        5373,
        111413,
        109587,
        46448,
        99250,
        104460,
        62926,
        111192,
        104748,
        1773,
        59151,
        99486,
        101916,
        107433,
        104723,
        1773
    ],

    "total_duration"3086970885,

    "load_duration"48652771,

    "prompt_eval_count"18,

    "prompt_eval_duration"53432000,

    "eval_count"90,

    "eval_duration"2943180000

}



这段代码使用了Python的requests库来发送HTTP POST请求,向本地服务器的API发送数据,并处理响应。接下来我会一步步解释这个代码,并用中文回答。

  1. 导入库:
import requests 
import json

代码首先导入了requests库用于发送HTTP请求,以及json库用于处理JSON数据。

  1. 定义URL和请求头:
url = "http://localhost:11434/api/generate"

headers = {
    "Content-Type": "application/json"
}

这里定义了请求的目标URL,即本地服务器上运行的API接口http://localhost:11434/api/generate。同时,定义了请求头,指定Content-Typeapplication/json,表示请求体中传递的数据格式为JSON。

  1. 创建请求体数据:
data = {
    "model": "qwen2:0.5b",
    "prompt": "Why is the sky black? Answer in Chinese.",
    "stream": False
}

data字典包含三个键值对:

  • "model": 指定使用的模型,这里是qwen2:0.5b
  • "prompt": 提供的提示问题,这里是“Why is the sky black? Answer in Chinese.”。
  • "stream": 表示是否希望服务器以流式方式返回数据,这里设为False
  1. 发送POST请求并获取响应:
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)

使用requests.post()方法发送POST请求,传递目标URL、请求头和请求体数据。然后打印服务器的响应内容。

  1. 处理响应:
if response.status_code == 200:
    response_text = response.text
    data = json.loads(response_text)
    actual_response = data["response"]
    print(actual_response)
else:
    print("error:", response.status_code, response.text)

首先检查响应的状态码是否为200(表示请求成功)。如果成功,解析响应文本为JSON格式,并提取实际的回答内容。否则,打印错误信息。

示例输出:

如果服务器正常运行并能处理请求,输出可能会是:

{
    "response": "因为天空在没有太阳的情况下没有被光线照亮,所以看起来是黑色的。"
}

程序会提取并打印 "因为天空在没有太阳的情况下没有被光线照亮,所以看起来是黑色的。"