Azure OpenAI API无法调用自定义数据源问题求助
解决Azure OpenAI添加自定义数据后API调用无法识别数据的问题
核心问题原因
Chat Playground会自动为「添加你的数据」功能注入专属配置参数,但手动API调用需要显式添加这些参数,否则模型只会使用基础能力,不会关联自定义数据源。
具体解决步骤
- 使用支持数据增强的API版本:必须调用
2023-10-01-preview及以上版本的Azure OpenAI API,旧版本不支持data_sources参数。 - 在API请求中添加
data_sources配置:这个参数要包含你绑定的数据源信息,示例结构如下:
把这个参数加入到聊天请求的data_sources = [ { "type": "azure_search", "parameters": { "endpoint": "你的Azure Cognitive Search端点", "key": "你的Search服务密钥", "index_name": "你的搜索索引名称", "in_scope": True, "role_information": "你的系统提示词(需和Playground里的一致)" } } ]extra_body(Python SDK)或者直接作为JSON字段传入API请求体。 - 核对模型部署与权限:确保API调用的是你在Playground中使用的同一个gpt-35-turbo部署,同时确认所用的Azure OpenAI密钥拥有访问该数据源(比如Azure Cognitive Search)的权限。
- 验证数据引用:调用成功后,检查响应中的
citations字段,如果返回了自定义数据源的片段,说明配置生效。
修正后的API调用示例(Python)
from openai import AzureOpenAI client = AzureOpenAI( azure_endpoint="你的Azure OpenAI端点", api_key="你的Azure OpenAI密钥", api_version="2023-10-01-preview" ) response = client.chat.completions.create( model="你的gpt-35-turbo部署名称", messages=[ {"role": "user", "content": "你的问题"} ], extra_body={ "data_sources": [ { "type": "azure_search", "parameters": { "endpoint": "https://xxx.search.windows.net", "key": "xxx", "index_name": "你的索引名", "in_scope": True, "role_information": "你设置的系统角色提示词" } } ] } ) print(response.choices[0].message.content) # 查看是否有自定义数据引用 print(response.choices[0].message.citations)
额外排查点
- 如果用的是Blob存储作为数据源,
data_sources的type要设为azure_blob,并对应填写Blob存储的端点、密钥、容器名等参数。 - 确认Playground中的系统提示词和API调用的
role_information完全一致,不一致可能导致模型行为差异。
内容的提问来源于stack exchange,提问作者Light Yagmi
相关产品推荐
相关产品推荐

