JSON 输出示例¶
此示例中包含两个 Python 脚本。randomaddresses.py
从不同国家生成随机地址。predefinedschema.py
设置模型填充的模板。
运行示例¶
1. 确保您已安装 llama2
模型:
2. 安装 Python 需求。
3. 运行随机地址示例:
4. 运行预定义模式示例:
查看代码¶
这两个程序基本相同,每个都有不同的提示,展示了两个不同的想法。从模型中获取 JSON 的关键部分是在提示或系统提示中声明它应该使用 JSON 响应,并在数据体中指定 format
为 json
。
prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in {country}, and phone number. Do not use common names. Respond using JSON. Key names should with no backslashes, values should use plain ascii with no special characters."
data = {
"prompt": prompt,
"model": model,
"format": "json",
"stream": False,
"options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}
当运行 randomaddresses.py
时,您会看到模式根据选择的国家而改变和适应。
在 predefinedschema.py
中,也在提示中指定了模板。它被定义为 JSON 然后转储到提示字符串中,使其更易于使用。
两个示例都关闭了流式传输,这样我们就可以一次性得到完整的 JSON。我们需要将 response.text
转换为 JSON,这样当我们将其输出为字符串时,我们可以设置缩进间距,使输出易于阅读。
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))
源码¶
predefinedschema.py¶
import requests
import json
import random
model = "llama2"
template = {
"firstName": "",
"lastName": "",
"address": {
"street": "",
"city": "",
"state": "",
"zipCode": ""
},
"phoneNumber": ""
}
prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in the US, and phone number. \nUse the following template: {json.dumps(template)}."
data = {
"prompt": prompt,
"model": model,
"format": "json",
"stream": False,
"options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}
print(f"Generating a sample user")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))
randomaddresses.py¶
import requests
import json
import random
countries = [
"United States",
"United Kingdom",
"the Netherlands",
"Germany",
"Mexico",
"Canada",
"France",
]
country = random.choice(countries)
model = "llama2"
prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in {country}, and phone number. Do not use common names. Respond using JSON. Key names should have no backslashes, values should use plain ascii with no special characters."
data = {
"prompt": prompt,
"model": model,
"format": "json",
"stream": False,
"options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}
print(f"Generating a sample user in {country}")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))