Llama2 Together AI 튜토리얼

Llama2 Together AI 튜토리얼

https://together.ai/

uv add litellm
import osfrom litellm import completionos.environ["TOGETHERAI_API_KEY"] = "" #@paramuser_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 모델에 대한 프롬프트 형식도 처리하여, 메시지를 요구되는 [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

커스텀 프롬프트 템플릿으로 Llama2 변형 사용

TogetherAI에서 커스텀 프롬프트 형식이 필요한 Llama2 버전을 사용하시나요?

커스텀 프롬프트 템플릿을 만들 수 있어요.

OpenAssistant/llama2-70b-oasst-sft-v10용으로 하나 만들어 봅시다!

허용되는 템플릿 형식은 다음과 같습니다: Reference

"""system{system_message}user{prompt}assistant"""

커스텀 프롬프트 템플릿을 등록해 보세요: 구현 코드

import litellm litellm.register_prompt_template(
    model="OpenAssistant/llama2-70b-oasst-sft-v10",
    roles={ # tell LiteLLM how you want to map the openai messages to this model
        "system": {"pre_message": "system\n", "post_message": "\n"},
        "user": {"pre_message": "user\n", "post_message": "\n"},
        "assistant": {"pre_message": "assistant\n", "post_message": "\n"}
    },
    final_prompt_value="assistant\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={ # tell LiteLLM how you want to map the openai messages to this model
        "system": {"pre_message": "system\n", "post_message": "\n"},
        "user": {"pre_message": "user\n", "post_message": "\n"},
        "assistant": {"pre_message": "assistant\n", "post_message": "\n"}
    },
    final_prompt_value="assistant\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"
}

더 알아보기 (Learn more)