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"}] ) 工作原理
模型会自动地根据你的输入进行内容的选择,主要的召回逻辑如下:
- 短文档直接传入GPT
- 对于长文档进行向量化召回
但是 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: 获取城市别名
推荐阅读
- C++的面向对象编程:深入解析与理解
- 深入掌握Java线程池调度策略,优化任务执行
- 探讨网站转化率与SEO优化之间的关系及提升策略
- 深入浅出Kafka:高可用、顺序消费及幂等性
- 人脸识别技术:应用与隐私挑战的探讨
- 62岁黄日华黄昏恋修成正果?手牵手甜蜜现身,与友人探讨婚礼细节
- 深入了解手机芯片:架构与性能的权衡
- 深入了解Linux中常见的五种文件类型
- 深入理解Java微服务架构与容器化部署
- 深入Rust的模式匹配与枚举类型
