通用对话
提供 OpenAI
兼容的对话生成接口。
重要变更
2025-03-20: 由于模型架构升级调整,原生的 联网搜索
能力已经下线。如果您的业务依赖 API 接口原生的 联网搜索
能力,请尽快切换到其他方案。
请求方法
POST
请求地址
https://chat.ecnu.edu.cn/open/api/v1/chat/completions
请求参数
参数名 | 参数类型 | 是否必选 | 描述 |
---|---|---|---|
messages | array | 是 | 对话消息,以 {"role": "user", "content": "你好"} 的json 数组形式进行传参; role 的取值范围包括 system ,user , assistant 三类。 |
stream | boolean | 否 | 是否开启流式对话 |
model | string | 是 | 模型名称,详见 模型介绍 |
temperature | float | 否 | 温度,取值范围 0-1,默认取值与模型有关 详见 模型介绍 |
string | 否 | 原生联网搜索能力已下线,请切换到 tools 调用或其他方案 | |
top_p | float | 否 | top-p ,取值范围 0-1,默认取值与模型有关 详见 模型介绍 |
tools | array | 否 | function call 的调用 |
tools.type | string | 是 | 工具类型,目前固定为 function |
tools.function | object | 是 | function call 的参数 |
tools.function.name | string | 是 | function call 的名称 |
tools.function.description | string | 是 | function call 的描述 |
tools.function.parameters | object | 是 | function call 的参数,详见请求示例 |
返回参数
参数名 | 参数类型 | 描述 |
---|---|---|
id | string | 对话id |
object | string | 返回对象类型 |
created | int | 对话创建时间 |
choices | array | 对话回复 |
choices.index | int | 回复索引 |
choices.message | object | 回复消息 |
choices.message.role | string | 回复消息角色 |
choices.message.reasoning_content | string | 思维链内容,仅 ecnu-reasoner 输出此字段 |
choices.message.content | string | 回复消息内容 |
choices.message.tool_calls | array | function call 的调用 |
choices.message.tool_calls.id | string | function call 的id |
choices.message.tool_calls.type | string | function call 的类型 |
choices.message.tool_calls.function | object | function call 的参数 |
choices.message.tool_calls.function.name | string | function call 的名称 |
choices.message.tool_calls.function.arguments | string | function call 的参数 |
choices.finish_reason | string | 对话结束原因 |
array | 原生联网搜索能力已下线,请切换到 tools 调用或其他方案 | |
usage | object | 对话使用情况 |
usage.prompt_tokens | int | 提示词 token 量(估计值) |
usage.completion_tokens | int | 回复的 token 量(估计值) |
usage.total_tokens | int | 总 token 量(估计值) |
请求示例
对话生成
- reqeust
http
POST https://chat.ecnu.edu.cn/open/api/v1/chat/completions
Content-Type: application/json
Authorization: Bearer yourapikey
{
"messages": [{
"role": "system",
"content": "你是华东师范大学大模型ChatECNU"
}, {
"role": "user",
"content": "你好呀"
}],
"stream": false,
"model": "ecnu-plus"
}
- response
json
{
"id": "chatcmpl-4fa713672569459d93bf3790b7a53179",
"object": "chat.completion",
"created": 1720671014,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么我可以帮助你的吗?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 8,
"total_tokens": 17
}
}
对话生成(流式)
- reqeust
http
POST https://chat.ecnu.edu.cn/open/api/v1/chat/completions
Content-Type: application/json
Authorization: Bearer yourapikey
{
"messages": [{
"role": "system",
"content": "你是华东师范大学大模型ChatECNU"
}, {
"role": "user",
"content": "你好呀"
}],
"stream": true,
"model": "ecnu-plus"
}
- response
r
data: {"id":"chatcmpl-137a302ac8c642d2bb6046fc0ae77b41","object":"chat.completion.chunk","created":1720671137,"model":"SparkDesk","choices":[{"index":0,"delta":{"content":"你好"}}]}
data: {"id":"chatcmpl-66c9b32131b04ebaa26ba29bc8938308","object":"chat.completion.chunk","created":1720671138,"model":"SparkDesk","choices":[{"index":0,"delta":{"content":"!很高兴"}}]}
data: {"id":"chatcmpl-98eabd5391fb4286bf160eedd460c2e1","object":"chat.completion.chunk","created":1720671138,"model":"SparkDesk","choices":[{"index":0,"delta":{"content":"为你提供帮助"}}]}
data: {"id":"chatcmpl-7add6aadb1644f269e550fc2df19b277","object":"chat.completion.chunk","created":1720671138,"model":"SparkDesk","choices":[{"index":0,"delta":{"content":"。"}}]}
data: {"id":"chatcmpl-86399dd5e06e4c3ab590b5684f5bdf9e","object":"chat.completion.chunk","created":1720671140,"model":"SparkDesk","choices":[{"index":0,"delta":{"content":"如果你有任何问题或者需要了解的信息,请随时告诉我。"},"finish_reason":"stop"}]}
data: [DONE]
function call
- request
http
POST https://chat.ecnu.edu.cn/open/api/v1/chat/completions
Content-Type: application/json
Authorization: Bearer yourapikey
{
"messages": [ {
"role": "user",
"content": "上海今天天气如何?"
}],
"tools":[
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当你想查询指定城市的天气时非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或县区,比如北京市、杭州市、余杭区等。"
}
}
},
"required": [
"location"
]
}
}
],
"stream": false,
"model": "ecnu-plus"
}
- response
json
{
"id": "chat-ca7f7b7aed434322a11e0350fc89802d",
"object": "chat.completion",
"created": 1732677065,
"model": "qwen25",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "chatcmpl-tool-4b247cc8f6db4565a574a3e1b30261f7",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\"location\": \"\\u4e0a\\u6d77\\u5e02\"}"
}
}
]
},
"logprobs": null,
"finish_reason": "tool_calls",
"stop_reason": null
}
],
"usage": {
"prompt_tokens": 186,
"total_tokens": 207,
"completion_tokens": 21
},
"prompt_logprobs": null
}
代码示例(对话)
直接调用接口
python
import requests
url = "https://chat.ecnu.edu.cn/open/api/v1/chat/completions"
headers = {
"Authorization":"Bearer your-api-key",# 替换为您的API密钥
"Content-Type":"application/json"
}
data = {
"model": "ecnu-plus",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"}
]
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
使用 openai sdk
python
import os
from openai import OpenAI
client = OpenAI(
api_key='your-api-key', # 替换为您的API密钥
base_url="https://chat.ecnu.edu.cn/open/api/v1",
)
completion = client.chat.completions.create(
model="ecnu-plus", # 模型列表:https://developer.ecnu.edu.cn/vitepress/llm/api/models.html
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你是谁?'}],
)
print(completion.model_dump_json())
使用 LangChain
python
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# 配置ChatECNU服务参数
llm = ChatOpenAI(
model="ecnu-plus",
openai_api_base="https://chat.ecnu.edu.cn/open/api/v1",
openai_api_key='your-api-key', # 替换为您的API密钥
)
# 创建对话模板
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("user", "{input}")
])
# 构建LLM链
chain = prompt | llm
# 执行对话
response = chain.invoke({"input": "你是谁?"})
# 打印结构化结果
print(response.model_dump_json())
代码示例(工具调用)
直接调用接口
python
import requests
import json
url = "https://chat.ecnu.edu.cn/open/api/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer yourapikey" # 替换为您的API密钥
}
payload = {
"messages": [{
"role": "user",
"content": "上海今天天气如何?"
}],
"tools": [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当你想查询指定城市的天气时非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或县区,比如北京市、杭州市、余杭区等。"
}
},
"required": ["location"]
}
}
}],
"model": "ecnu-plus"
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
print(response.json())
使用 openai sdk
python
from openai import OpenAI
client = OpenAI(
base_url="https://chat.ecnu.edu.cn/open/api/v1",
api_key="yourapikey" # 替换为您的API密钥
)
response = client.chat.completions.create(
model="ecnu-plus",
messages=[{
"role": "user",
"content": "上海今天天气如何?"
}],
tools=[{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当你想查询指定城市的天气时非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或县区,比如北京市、杭州市、余杭区等。"
}
},
"required": ["location"]
}
}
}]
)
print(response)
使用 LangGraph
python
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
# 配置模型
llm = ChatOpenAI(
model="ecnu-plus",
openai_api_base="https://chat.ecnu.edu.cn/open/api/v1",
openai_api_key="yourapikey" # 替换为您的真实API密钥
)
agent = create_react_agent(
model=llm,
tools=[get_weather],
prompt="You are a helpful assistant"
)
# Run the agent
response = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in shanghai?"}]}
)
print(response)