复制
向 AI 提问
import openai
from langchain_community.adapters import openai as lc_openai
chat.completions.create
复制
向 AI 提问
messages = [{"role": "user", "content": "hi"}]
复制
向 AI 提问
result = openai.chat.completions.create(
messages=messages, model="gpt-3.5-turbo", temperature=0
)
result.choices[0].message.model_dump()
复制
向 AI 提问
{'content': 'Hello! How can I assist you today?',
'role': 'assistant',
'function_call': None,
'tool_calls': None}
复制
向 AI 提问
lc_result = lc_openai.chat.completions.create(
messages=messages, model="gpt-3.5-turbo", temperature=0
)
lc_result.choices[0].message # Attribute access
复制
向 AI 提问
{'role': 'assistant', 'content': 'Hello! How can I help you today?'}
复制
向 AI 提问
lc_result["choices"][0]["message"] # Also compatible with index access
复制
向 AI 提问
{'role': 'assistant', 'content': 'Hello! How can I help you today?'}
复制
向 AI 提问
lc_result = lc_openai.chat.completions.create(
messages=messages, model="claude-2", temperature=0, provider="ChatAnthropic"
)
lc_result.choices[0].message
复制
向 AI 提问
{'role': 'assistant', 'content': 'Hello! How can I assist you today?'}
chat.completions.stream
原始 OpenAI 调用复制
向 AI 提问
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())
复制
向 AI 提问
{'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}
复制
向 AI 提问
for c in lc_openai.chat.completions.create(
messages=messages, model="gpt-3.5-turbo", temperature=0, stream=True
):
print(c.choices[0].delta)
复制
向 AI 提问
{'role': 'assistant', 'content': ''}
{'content': 'Hello'}
{'content': '!'}
{'content': ' How'}
{'content': ' can'}
{'content': ' I'}
{'content': ' assist'}
{'content': ' you'}
{'content': ' today'}
{'content': '?'}
{}
复制
向 AI 提问
for c in lc_openai.chat.completions.create(
messages=messages,
model="claude-2",
temperature=0,
stream=True,
provider="ChatAnthropic",
):
print(c["choices"][0]["delta"])
复制
向 AI 提问
{'role': 'assistant', 'content': ''}
{'content': 'Hello'}
{'content': '!'}
{'content': ' How'}
{'content': ' can'}
{'content': ' I'}
{'content': ' assist'}
{'content': ' you'}
{'content': ' today'}
{'content': '?'}
{}
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。