LangChain E-Mails with LLM

题意:通过LangChain使用大型语言模型(LLM)处理电子邮件

问题背景:

I am quite new to LangChain and Python as im mainly doing C# but i am interested in using AI on my own data. So i wrote some python code using langchain that:

我对于LangChain和Python比较新,因为我主要做C#,但我对在自己的数据上使用AI很感兴趣。因此,我使用LangChain编写了一些Python代码,这些代码可以

1. Gets my Emails via IMAP        通过IMAP获取我的电子邮件

2. Creates JSON from my E-Mails (JSONLoader)  

从我的电子邮件中创建JSON(JSONLoader)

3. Creates a Vectordatabase where each mail is a vector (FAISS, OpenAIEmbeddings)

创建一个向量数据库,其中每封邮件都是一个向量(使用FAISS和OpenAI嵌入)

4. Does a similarity search according to the query returning the 3 mails that match the query the most

根据查询进行相似度搜索,返回与查询最匹配的3封邮件

5. feeds the result of the similarity search to the LLM (GPT 3.5 Turbo) using the query AGAIN

再次使用查询将相似度搜索的结果提供给LLM(GPT 3.5 Turbo)

The LLM Prompt then looks something like:        然后LLM的提示(Prompt)看起来像这样:

The question is{query}Here are some information that can help you to answer the question:{similarity_search_result}

Ok so far so good... when my question is:        好的,目前为止还不错……当我的问题是:

When was my last mail sent to xyz@gmail.com?

我最后一次给mailto:xyz@gmail.com发送邮件是什么时候?

i get a correct answer... -> e.g last mail received 10.04.2024 14:11

我得到了一个正确的答案……例如,最后一封邮件的接收时间是2024年10月4日14:11。

But what if i want to have an answer to the following question

但如果我想得到以下问题的答案呢?

How many mails have been sent by xyz@gmail.com?

mailto:xyz@gmail.com发送了多少封邮件?即 xyz@gmail.com 接收了多少邮件?

Because the similarity search only gets the vectors that are most similar, how can i just get an answer about the amount? Even if the similarity search would deliver 150 mails instead of 3 sent by xyz@gmail.com i cant just feed them all into the LLM prompt right?

因为相似度搜索只获取最相似的向量,我如何只得到数量的答案呢?即使相似度搜索返回了由xyz@gmail.com发送的150封邮件而不是3封,我也不能直接将它们全部输入到LLM的提示中,对吧?

So what is my mistake here?        那么我在这里的错误是什么?

问题解决:

It sounds like you need what OpenAI calls "function calling" / tools. RAG is great for grabbing relevant documents to dump into the context window, but as you've seen it's not suitable for everything. Thankfully, we can add arbitrary capabilities without implementing our own hacky solution using function calling. You first implement a function that does what you want in python. When you query OpenAI, you provide a description of these functions (tools). The chat completions API can reason about your request, then respond with JSON containing arguments for you to pass to the function you defined. This allows llms to hypothetically take any actions a human would.

听起来你需要的是OpenAI所说的“函数调用”/工具。RAG(Retrieval Augmented Generation,检索增强生成)在抓取相关文档并放入上下文窗口中非常出色,但正如你所看到的,它并不适合所有情况。幸运的是,我们可以使用函数调用来添加任意功能,而无需实现我们自己的粗暴解决方案。你首先在Python中实现一个你想要的函数。当你向OpenAI发出查询时,你提供这些函数(工具)的描述。聊天补全API可以根据你的请求进行推理,然后返回包含参数的JSON,这些参数需要你传递给你定义的函数。这允许LLMs(大型语言模型)理论上执行人类可以执行的任何操作。

So, for your case of getting the number of emails by email address, you'd want to implement a function in python that perhaps queries for the number of emails for a given user via IMAP. I'll leave that task to you, but once you complete that, the below should serve as a working minimal example to build off of.

因此,对于你想要通过电子邮件地址获取邮件数量的情况,你需要在Python中实现一个函数,该函数可能通过IMAP查询给定用户的邮件数量。我将把这个任务留给你,但一旦你完成了这个功能,下面的内容应该可以作为一个可以构建的最小工作示例。

import json
from openai import OpenAIclient = OpenAI(api_key='YOUR API KEY')tools = [{"type": "function","function": {"name": "total_number_of_emails","description": "Get the number of emails in an email user's inbox","parameters": {"type": "object","properties": {"email_address": {"type": "string","description": "The user's email address",},},"required": ["email_address"],},},},
]def total_number_of_emails(email_address):return 42 # replace with real code to grab # of emailsdef test(query):cpl = client.chat.completions.create(model='gpt-3.5-turbo',messages=[{'role': 'user', 'content': query}],tools=tools,tool_choice='auto' # lets model decide whether to use a tool)for tool_call in cpl.choices[0].message.tool_calls:fn = tool_call.functionif fn.name == 'total_number_of_emails':args = json.loads(fn.arguments)print(total_number_of_emails(args['email_address']))test('How many mails have been sent by xyz@gmail.com?')

If you simply copy and paste the above code unmodified, add your api key, and execute it, it should print "42" every time.

如果你直接复制和粘贴上面的代码而不做任何修改,然后添加你的API密钥并执行它,它应该每次都打印出“42”。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/37542.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何安装和卸载软件?

如何安装和卸载软件? 💻 如何安装和卸载软件?——默语的详细教程摘要引言正文内容🖥️ 在Windows上安装和卸载软件安装软件卸载软件 🍏 在Mac上安装和卸载软件安装软件卸载软件 🤔 QA环节📝 表格…

数据结构和算法,单链表的实现(kotlin版)

数据结构和算法&#xff0c;单链表的实现(kotlin版) 1.定义接口&#xff0c;我们需要实现的方法 interface LinkedListAction<E> {fun push(e: E)fun size(): Intfun getValue(index: Int): E?fun insert(index: Int,e: E)fun remove(index: Int) }2.定义节点&#xf…

使用Java连接数据库并且执行数据库操作和创建用户登录图形化界面(2)

(1)在student数据库上创建一个用户表tb_account,该表包含用户id,用户名和密码。 字段名称 数据类型 注释 约束 user_id Char(8)

Redis+定式任务实现简易版消息队列

Redis是一个开源的内存中数据结构存储系统&#xff0c;通常被用作数据库、缓存和消息中间件。 Redis主要将数据存储在内存中&#xff0c;因此读写速度非常快。 支持不同的持久化方式&#xff0c;可以将内存中的数据定期写入磁盘&#xff0c;保证数据持久性。 redis本身就有自己…

C++容器使用详解---vector容器

文章目录 1.vector容器1.vector容器构造函数2.vector容器的赋值操作3.vector容器的容量和大小4.vector容器的插入和删除5.vector容器的数据存取6.vector容器互换容器7.vector容器预留空间 1.vector容器 std::vector是C标准模板库(STL)中的一个动态数组容器&#xff0c;其内部实…

QT QThread 线程类的使用及示例

QThread 是 Qt 框架提供的一个用于处理多线程的类&#xff0c;它允许开发者编写具有并发功能的应用程序&#xff0c;提高程序的响应速度、执行效率和用户体验。 在操作系统中&#xff0c;线程是进程内的执行单元&#xff0c;拥有独立的执行路径。每个线程有自己独立的栈空间&a…

从零开始学Spring Boot系列-集成Spring Security实现用户认证与授权

在Web应用程序中&#xff0c;安全性是一个至关重要的方面。Spring Security是Spring框架的一个子项目&#xff0c;用于提供安全访问控制的功能。通过集成Spring Security&#xff0c;我们可以轻松实现用户认证、授权、加密、会话管理等安全功能。本篇文章将指导大家从零开始&am…

Oracle day15

/*create table f0307 ( id number ,productname varchar2(100) ,parentid number); insert into f0307 values ( 1,汽车,null); insert into f0307 values ( 2,车身,1); insert into f0307 values ( 3,发动机,1); insert into f0307 values ( 4,车门,2); insert into f0307 va…

日期类(java)

文章目录 第一代日期类 Date常用构造方法SimpleDateFormat 日期格式化类日期转字符串&#xff08;String -> Date)字符串转日期 (String->Date) 第二代日期类 Calendar常用字段与如何得到实例对象相关 API 第三代日期类&#xff08;LocalDate\TIme)日期&#xff0c;时间&…

springboot + Vue前后端项目(第二十一记)

项目实战第二十一记 写在前面1. springboot文件默认传输限制2. 安装视频插件包命令3. 前台Video.vue4. 创建视频播放组件videoDetail.vue5. 路由6. 效果图总结写在最后 写在前面 本篇主要讲解系统集成视频播放插件 1. springboot文件默认传输限制 在application.yml文件中添…

pip安装neuralcoref失败ERROR

最终解决的方法如下&#xff1a; git clone https://github.com/huggingface/neuralcoref.git cd neuralcoref pip install -r requirements.txt python setup.py install 原始步骤&#xff1a; 安装 neuralcoref 的依赖&#xff1a; 安装编译 neuralcoref 所需的依赖项&am…

PHP If...Else 语句的深入解析

PHP If...Else 语句的深入解析 在PHP编程语言中&#xff0c;if...else 语句是一种基本且强大的控制结构&#xff0c;它允许根据特定条件执行不同的代码块。这种结构对于决策制定和流程控制至关重要&#xff0c;是每位PHP开发者必须熟练掌握的内容。本文将详细探讨if...else语句…

boost asio异步服务器(4)处理粘包

粘包的产生 当客户端发送多个数据包给服务器时&#xff0c;服务器底层的tcp接收缓冲区收到的数据为粘连在一起的。这种情况的产生通常是服务器端处理数据的速率不如客户端的发送速率的情况。比如&#xff1a;客户端1s内连续发送了两个hello world&#xff01;,服务器过了2s才接…

MCU解决800V电动汽车牵引逆变器的常见设计挑战的3种方式

电动汽车 (EV) 牵引逆变器是电动汽车的。它将高压电池的直流电转换为多相&#xff08;通常为三相&#xff09;交流电以驱动牵引电机&#xff0c;并控制制动产生的能量再生。电动汽车电子产品正在从 400V 转向 800V 架构&#xff0c;这有望实现&#xff1a; 快速充电 – 在相同…

WPF 2024 金九银十 最新 高级 架构 面试题 C#

含入门 初级 中级 高级 不同级别WPF的面试题 相关面试题 redis安装说明书 http://t.csdnimg.cn/iM260 单体并发瓶颈 redis sqlsever mysql多少 http://t.csdnimg.cn/DTXIh Redis高频面试题http://t.csdnimg.cn/FDOnv 数据库SqlServer笔试题 数据库SqlServer笔试题-CSDN博客 SQL…

绝了!Stable Diffusion做AI治愈图片视频,用来做副业简直无敌!10分钟做一个爆款视频保姆教程

一 项目分析 这个治愈类视频的玩法是通过AI生成日常生活场景&#xff0c;制作的vlog&#xff0c;有这样的一个号&#xff0c;发布了几条作品&#xff0c;就涨粉了2000多&#xff0c;点赞7000多&#xff0c;非常的受欢迎。 下面给大家看下这种作品是什么样的&#xff0c;如图所…

探索高效开发神器:Blackbox AI(免费编程助手)

人不走空 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌赋&#xff1a;斯是陋室&#xff0c;惟吾德馨 &#x1f916; 想要代码生成&#xff1f;&#x1f44c; &#x1f4ac; 需要和AI聊天解决难题&#xff1f;&#…

Ubuntu使用c++

Ubuntu使用c 一、安装编译器和开发工具二、创建一个c文件并运行1.创建一个c2.编译运行 一、安装编译器和开发工具 先安装vim sudo apt install vim再安装GUN编译器合集&#xff08;GCC&#xff09; sudo apt install build-essential使用g -v查看版本确定安装成功 二、创建…

Javaweb-初学

1.利用springboot开发一个web应用 简要流程&#xff0c;如下图 2.如何创建一个springboot的项目&#xff0c;下面两张图片是重点关注 第一张图片记得和图片一样改一下路径 第二张就是勾一个选项 3.第一个简单的springboot应用的开发 根据如下步骤进行操作 首先顶部要标识Res…

TensorFlow的学习1.2-基本概念

TensorFlow的学习2-基本概念 1. 张量&#xff08;Tensor&#xff09;2. 变量&#xff08;Variable&#xff09;3. 操作&#xff08;Operation&#xff09;4. 计算图&#xff08;Computational Graph&#xff09;5. 会话&#xff08;Session&#xff09;6. Eager Execution7. 数…