Skip to main content
DeepInfra다양한 LLM임베딩 모델에 대한 액세스를 제공하는 서버리스 추론 as a service입니다. 이 노트북은 채팅 모델에 DeepInfra와 함께 LangChain을 사용하는 방법을 다룹니다.

환경 API 키 설정

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

import os
from getpass import getpass

from langchain_community.chat_models import ChatDeepInfra
from langchain.messages import HumanMessage

DEEPINFRA_API_TOKEN = getpass()

# or pass deepinfra_api_token parameter to the ChatDeepInfra constructor
os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN

chat = ChatDeepInfra(model="meta-llama/Llama-2-7b-chat-hf")

messages = [
    HumanMessage(
        content="Translate this sentence from English to French. I love programming."
    )
]
chat.invoke(messages)

ChatDeepInfra는 비동기 및 스트리밍 기능도 지원합니다

from langchain_core.callbacks import StreamingStdOutCallbackHandler
await chat.agenerate([messages])
chat = ChatDeepInfra(
    streaming=True,
    verbose=True,
    callbacks=[StreamingStdOutCallbackHandler()],
)
chat.invoke(messages)

도구 호출

DeepInfra는 현재 invoke 및 async invoke 도구 호출만 지원합니다. 도구 호출을 지원하는 전체 모델 목록은 도구 호출 문서를 참조하세요.
import asyncio

from dotenv import find_dotenv, load_dotenv
from langchain_community.chat_models import ChatDeepInfra
from langchain.messages import HumanMessage
from langchain.tools import tool
from pydantic import BaseModel

model_name = "meta-llama/Meta-Llama-3-70B-Instruct"

_ = load_dotenv(find_dotenv())


# LangChain tool
@tool
def foo(something):
    """
    Called when foo
    """
    pass


# Pydantic class
class Bar(BaseModel):
    """
    Called when Bar
    """

    pass


llm = ChatDeepInfra(model=model_name)
tools = [foo, Bar]
llm_with_tools = llm.bind_tools(tools)
messages = [
    HumanMessage("Foo and bar, please."),
]

response = llm_with_tools.invoke(messages)
print(response.tool_calls)
# [{'name': 'foo', 'args': {'something': None}, 'id': 'call_Mi4N4wAtW89OlbizFE1aDxDj'}, {'name': 'Bar', 'args': {}, 'id': 'call_daiE0mW454j2O1KVbmET4s2r'}]


async def call_ainvoke():
    result = await llm_with_tools.ainvoke(messages)
    print(result.tool_calls)


# Async call
asyncio.run(call_ainvoke())
# [{'name': 'foo', 'args': {'something': None}, 'id': 'call_ZH7FetmgSot4LHcMU6CEb8tI'}, {'name': 'Bar', 'args': {}, 'id': 'call_2MQhDifAJVoijZEvH8PeFSVB'}]

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