OpenAI 介面卡
請確保 OpenAI 函式庫版本為 1.0.0 或更高版本;否則,請參考舊版文件 OpenAI 介面卡(舊版)。
許多人從 OpenAI 開始,但想探索其他模型。LangChain 與眾多模型供應商的整合讓這件事變得容易。雖然 LangChain 有自己的訊息和模型 API,但我們也盡可能讓探索其他模型變得容易,透過公開一個介面卡,將 LangChain 模型調整為 OpenAI API。
目前這僅處理輸出,不傳回其他資訊(token 計數、停止原因等)。
import openai
from langchain_community.adapters import openai as lc_openai
API 參考:openai
chat.completions.create
messages = [{"role": "user", "content": "hi"}]
原始 OpenAI 呼叫
result = openai.chat.completions.create(
messages=messages, model="gpt-3.5-turbo", temperature=0
)
result.choices[0].message.model_dump()
{'content': 'Hello! How can I assist you today?',
'role': 'assistant',
'function_call': None,
'tool_calls': None}
LangChain OpenAI 封裝器呼叫
lc_result = lc_openai.chat.completions.create(
messages=messages, model="gpt-3.5-turbo", temperature=0
)
lc_result.choices[0].message # Attribute access
{'role': 'assistant', 'content': 'Hello! How can I help you today?'}
lc_result["choices"][0]["message"] # Also compatible with index access
{'role': 'assistant', 'content': 'Hello! How can I help you today?'}
替換模型供應商
lc_result = lc_openai.chat.completions.create(
messages=messages, model="claude-2", temperature=0, provider="ChatAnthropic"
)
lc_result.choices[0].message
{'role': 'assistant', 'content': 'Hello! How can I assist you today?'}
chat.completions.stream
原始 OpenAI 呼叫
for c in openai.chat.completions.create(
messages=messages, model="gpt-3.5-turbo", temperature=0, stream=True
):
print(c.choices[0].delta.model_dump())
{'content': '', 'function_call': None, 'role': 'assistant', 'tool_calls': None}
{'content': 'Hello', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': '!', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': ' How', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': ' can', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': ' I', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': ' assist', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': ' you', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': ' today', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': '?', 'function_call': None, 'role': None, 'tool_calls': None}
{'content': None, 'function_call': None, 'role': None, 'tool_calls': None}
LangChain OpenAI 封裝器呼叫
for c in lc_openai.chat.completions.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.chat.completions.create(
messages=messages,
model="claude-2",
temperature=0,
stream=True,
provider="ChatAnthropic",
):
print(c["choices"][0]["delta"])
{'role': 'assistant', 'content': ''}
{'content': 'Hello'}
{'content': '!'}
{'content': ' How'}
{'content': ' can'}
{'content': ' I'}
{'content': ' assist'}
{'content': ' you'}
{'content': ' today'}
{'content': '?'}
{}