Llama2 Together AI 教程
!pip install litellm
import os
from litellm import completion
os.environ["TOGETHERAI_API_KEY"] = "" #@param
user_message = "Hello, whats the weather in San Francisco??"
messages = [{ "content": user_message,"role": "user"}]
在 TogetherAI 上调用 Llama2
https://api.together.xyz/playground/chat?model=togethercomputer%2Fllama-2-70b-chat
model_name = "together_ai/togethercomputer/llama-2-70b-chat"
response = completion(model=model_name, messages=messages)
print(response)
{'choices': [{'finish_reason': 'stop', 'index': 0, 'message': {'role': 'assistant', 'content': "\n\nI'm not able to provide real-time weather information. However, I can suggest"}}], 'created': 1691629657.9288375, 'model': 'togethercomputer/llama-2-70b-chat', 'usage': {'prompt_tokens': 9, 'completion_tokens': 17, 'total_tokens': 26}}
LiteLLM 也处理 Together AI 的 Llama2 模型的 prompt 格式,将你的消息转换为所需的 [INST] <your instruction> [/INST]
格式。
使用流式传输
response = completion(model=model_name, messages=messages, together_ai=True, stream=True)
print(response)
for chunk in response:
print(chunk['choices'][0]['delta']) # same as openai format
使用自定义 Prompt 模板配合 Llama2 变体
在 TogetherAI 上使用需要自定义 prompt 格式的 Llama2 版本?
你可以创建一个自定义 prompt 模板。
让我们为 OpenAssistant/llama2-70b-oasst-sft-v10
创建一个!
接受的模板格式为: 参考
"""
<|im_start|>system
{system_message}<|im_end|>
<|im_start|>user
{prompt}<|im_end|>
<|im_start|>assistant
"""
让我们注册我们的自定义 prompt 模板: 实现代码
import litellm
litellm.register_prompt_template(
model="OpenAssistant/llama2-70b-oasst-sft-v10",
roles={"system":"<|im_start|>system", "assistant":"<|im_start|>assistant", "user":"<|im_start|>user"}, # tell LiteLLM how you want to map the openai messages to this model
pre_message_sep= "\n",
post_message_sep= "\n"
)
让我们使用它!
from litellm import completion
# set env variable
os.environ["TOGETHERAI_API_KEY"] = ""
messages=[{"role":"user", "content": "Write me a poem about the blue sky"}]
completion(model="together_ai/OpenAssistant/llama2-70b-oasst-sft-v10", messages=messages)
完整代码
import litellm
from litellm import completion
# set env variable
os.environ["TOGETHERAI_API_KEY"] = ""
litellm.register_prompt_template(
model="OpenAssistant/llama2-70b-oasst-sft-v10",
roles={"system":"<|im_start|>system", "assistant":"<|im_start|>assistant", "user":"<|im_start|>user"}, # tell LiteLLM how you want to map the openai messages to this model
pre_message_sep= "\n",
post_message_sep= "\n"
)
messages=[{"role":"user", "content": "Write me a poem about the blue sky"}]
response = completion(model="together_ai/OpenAssistant/llama2-70b-oasst-sft-v10", messages=messages)
print(response)
输出
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": ".\n\nThe sky is a canvas of blue,\nWith clouds that drift and move,",
"role": "assistant",
"logprobs": null
}
}
],
"created": 1693941410.482018,
"model": "OpenAssistant/llama2-70b-oasst-sft-v10",
"usage": {
"prompt_tokens": 7,
"completion_tokens": 16,
"total_tokens": 23
},
"litellm_call_id": "f21315db-afd6-4c1e-b43a-0b5682de4b06"
}