深入探讨GPTs和AI Assistant( 五 )


import openai as client assistant = client.beta.assistants.create( instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.", model="gpt-4-1106-preview", tools=[{"type": "code_interpreter"}] )
模型之后会选择是否使用 Code Interpreter 去运行用户的请求 。
在 Code Interpreter 中传入文件
Code Interpreter 可以解析多种不同类型的文件 , 所以当你需要处理大量的数据时 , AI Assistant 允许你传入自己的文件进行分析 。
注意:上传的文件需要设置 purpose='assistants'
# Upload a file with an "assistants" purpose import openai as client file = client.files.create( file=open("speech.py", "rb"), purpose='assistants' ) # Create an assistant using the file ID assistant = client.beta.assistants.create( instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.", model="gpt-4-1106-preview", tools=[{"type": "code_interpreter"}], file_ids=[file.id] )
如果需要指定 对话级别的文件访问(即改文件只在这个对话中可以被访问) , 则可以使用如下的代码:
thread = client.beta.threads.create( messages=[ { "role": "user", "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?", "file_ids": [file.id] } ] )
文件最大可以支持512 MB , z支持的格式包含 .csv, .pdf, .json 和其他格式
知识库获取 Knowledge Retrieval
知识库获取是克服 ChatGPT 知识储备时效性问题 , 以及数据私有化的有效手段,例如利用知识库获取能力,可以把业务数据知识库集成到GPT中 。
开发者可以将文件(知识库)上传到AI 助手中,Open AI 会自动化对文档进行分块,加索引(index)以及embedding存储和实现向量化检索 。
所以不需要用户自己进行这一系列操作就可以完成知识库检索的能力 。
开启知识库检索
Assistant 如果需要开启知识库增强,只需要在初始化中的 tools加入 tools=[{"type": "retrieval"}]参数 。
assistant = client.beta.assistants.create( instructions="You are a customer support chatbot. Use your knowledge base to best respond to customer queries.", model="gpt-4-1106-preview", tools=[{"type": "retrieval"}] ) 工作原理
模型会自动地根据你的输入进行内容的选择,主要的召回逻辑如下:

  1. 短文档直接传入GPT
  2. 对于长文档进行向量化召回
Function Calling跟 ChatGPT 的 Completion API 一样,Assistant API 也支持 function calling 。Function Calling 允许你将函数的描述告诉AI 助手,包含了函数的定义以及参数等 , 然后 AI 助手会智能调用 。
但是 Assistant API 不会直接调用函数 , 而是将函数的参数和函数返回,等待你提交函数调用结果,才会进行下一步的执行 。
定义函数
首先需要按照如下的样例递交函数定义
{ "type": "function", # 类型一定是function "function": { "name": "getCurrentWeather", # 函数名 "deion": "Get the weather in location", #函数的描述 "parameters": { # 函数的参数 "type": "object", "properties": { "location": {"type": "string", "deion": "The city and state e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["c", "f"]} }, "required": ["location"] } } }
然后将函数的参入 Assistant API的tools 参数中 。例如下面的例子 , 定义了一个天气机器人,可以获取天气信息 。包含了两个函数:
  • getCurrentWeather:获取城市的天气
  • getNickname: 获取城市别名
assistant = client.beta.assistants.create( instructions="You are a weather bot. Use the provided functions to answer questions.", model="gpt-4-1106-preview", tools=[{ "type": "function", "function": { "name": "getCurrentWeather", "deion": "Get the weather in location", "parameters": { "type": "object", "properties": { "location": {"type": "string", "deion": "The city and state e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["c", "f"]} }, "required": ["location"] } } }, { "type": "function", "function": { "name": "getNickname", "deion": "Get the nickname of a city", "parameters": { "type": "object", "properties": { "location": {"type": "string", "deion": "The city and state e.g. San Francisco, CA"}, }, "required": ["location"] } } }] ) 获取调用的函数当 初始化一个执行(Run) 的时候,如果调用了一个function , 则会进入到 pending的状态 。需要你进行提交函数的结果 。


推荐阅读