DeepInfra다양한 LLMembedding 모델에 대한 액세스를 제공하는 서버리스 추론 서비스입니다. 이 노트북은 언어 모델을 위해 LangChain을 DeepInfra와 함께 사용하는 방법을 다룹니다.

Environment API Key 설정

DeepInfra에서 API key를 받아야 합니다. 로그인하여 새 토큰을 받으세요. 다양한 모델을 테스트할 수 있도록 1시간의 무료 서버리스 GPU 컴퓨팅이 제공됩니다. (여기 참조) deepctl auth token 명령으로 토큰을 출력할 수 있습니다.
# get a new token: https://deepinfra.com/login?from=%2Fdash

from getpass import getpass

DEEPINFRA_API_TOKEN = getpass()
 ········
import os

os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN

DeepInfra 인스턴스 생성

오픈소스 deepctl 도구를 사용하여 모델 배포를 관리할 수도 있습니다. 사용 가능한 매개변수 목록은 여기에서 확인할 수 있습니다.
from langchain_community.llms import DeepInfra

llm = DeepInfra(model_id="meta-llama/Llama-2-70b-chat-hf")
llm.model_kwargs = {
    "temperature": 0.7,
    "repetition_penalty": 1.2,
    "max_new_tokens": 250,
    "top_p": 0.9,
}
# run inferences directly via wrapper
llm("Who let the dogs out?")
'This is a question that has puzzled many people'
# run streaming inference
for chunk in llm.stream("Who let the dogs out?"):
    print(chunk)
 Will
 Smith
.

Prompt Template 생성

질문과 답변을 위한 prompt template을 생성합니다.
from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)

LLMChain 초기화

from langchain.chains import LLMChain

llm_chain = LLMChain(prompt=prompt, llm=llm)

LLMChain 실행

질문을 제공하고 LLMChain을 실행합니다.
question = "Can penguins reach the North pole?"

llm_chain.run(question)
"Penguins are found in Antarctica and the surrounding islands, which are located at the southernmost tip of the planet. The North Pole is located at the northernmost tip of the planet, and it would be a long journey for penguins to get there. In fact, penguins don't have the ability to fly or migrate over such long distances. So, no, penguins cannot reach the North Pole. "

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I