简单生成示例¶
这是一个使用 Generate 端点的简单示例。
运行示例¶
1. 确保您已安装 stablelm-zephyr
模型:
2. 安装Python所需库。
3. 运行示例:
代码审查¶
main 函数仅请求输入,然后将其传递给 generate 函数。然后,从 generate 返回的输出在下一次运行时再次传回 generate。
generate 函数使用 requests.post
来调用 /api/generate
,传递模型、提示和上下文。generate
端点返回一串 JSON 块,然后迭代这些块,寻找响应值。然后将其打印出来。最终的JSON对象包含到目前为止对话的完整上下文,并且这是该函数的返回值。
源码¶
client.py¶
import json
import requests
# NOTE: ollama must be running for this to work, start the ollama app or run `ollama serve`
model = 'stablelm-zephyr' # TODO: update this for whatever model you wish to use
def generate(prompt, context):
r = requests.post('http://localhost:11434/api/generate',
json={
'model': model,
'prompt': prompt,
'context': context,
},
stream=True)
r.raise_for_status()
for line in r.iter_lines():
body = json.loads(line)
response_part = body.get('response', '')
# the response streams one token at a time, print that as we receive it
print(response_part, end='', flush=True)
if 'error' in body:
raise Exception(body['error'])
if body.get('done', False):
return body['context']
def main():
context = [] # the context stores a conversation history, you can use this to make the model more context aware
while True:
user_input = input("Enter a prompt: ")
if not user_input:
exit()
print()
context = generate(user_input, context)
print()
if __name__ == "__main__":
main()