Skip to main content
OpenAI 라이브러리 버전이 1.0.0 미만인지 확인하세요. 그렇지 않은 경우 최신 문서 OpenAI Adapter를 참조하세요. 많은 사람들이 OpenAI로 시작하지만 다른 모델을 탐색하고 싶어합니다. LangChain은 다양한 모델 공급자와 통합되어 있어 이를 쉽게 수행할 수 있습니다. LangChain은 자체 메시지 및 모델 API를 가지고 있지만, LangChain 모델을 OpenAI API에 적응시키는 어댑터를 제공하여 다른 모델을 최대한 쉽게 탐색할 수 있도록 했습니다. 현재 이 기능은 출력만 처리하며 다른 정보(토큰 수, 중지 이유 등)는 반환하지 않습니다.
import openai
from langchain_community.adapters import openai as lc_openai

ChatCompletion.create

messages = [{"role": "user", "content": "hi"}]
원본 OpenAI 호출
result = openai.ChatCompletion.create(
    messages=messages, model="gpt-3.5-turbo", temperature=0
)
result["choices"][0]["message"].to_dict_recursive()
{'role': 'assistant', 'content': 'Hello! How can I assist you today?'}
LangChain OpenAI 래퍼 호출
lc_result = lc_openai.ChatCompletion.create(
    messages=messages, model="gpt-3.5-turbo", temperature=0
)
lc_result["choices"][0]["message"]
{'role': 'assistant', 'content': 'Hello! How can I assist you today?'}
모델 공급자 교체하기
lc_result = lc_openai.ChatCompletion.create(
    messages=messages, model="claude-2", temperature=0, provider="ChatAnthropic"
)
lc_result["choices"][0]["message"]
{'role': 'assistant', 'content': ' Hello!'}

ChatCompletion.stream

원본 OpenAI 호출
for c in openai.ChatCompletion.create(
    messages=messages, model="gpt-3.5-turbo", temperature=0, stream=True
):
    print(c["choices"][0]["delta"].to_dict_recursive())
{'role': 'assistant', 'content': ''}
{'content': 'Hello'}
{'content': '!'}
{'content': ' How'}
{'content': ' can'}
{'content': ' I'}
{'content': ' assist'}
{'content': ' you'}
{'content': ' today'}
{'content': '?'}
{}
LangChain OpenAI 래퍼 호출
for c in lc_openai.ChatCompletion.create(
    messages=messages, model="gpt-3.5-turbo", temperature=0, stream=True
):
    print(c["choices"][0]["delta"])
{'role': 'assistant', 'content': ''}
{'content': 'Hello'}
{'content': '!'}
{'content': ' How'}
{'content': ' can'}
{'content': ' I'}
{'content': ' assist'}
{'content': ' you'}
{'content': ' today'}
{'content': '?'}
{}
모델 공급자 교체하기
for c in lc_openai.ChatCompletion.create(
    messages=messages,
    model="claude-2",
    temperature=0,
    stream=True,
    provider="ChatAnthropic",
):
    print(c["choices"][0]["delta"])
{'role': 'assistant', 'content': ' Hello'}
{'content': '!'}
{}

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