当前位置: 首页 > news >正文

Elasticsearch:使用 Gradio 来创建一个简单的 RAG 应用界面

Gradio 是用最快的方式为你的机器学习模型制作一个友好的网页界面,让任何人都能在任何地方使用它!最近看了一两个例子。Gradio 的实现非常简单粗暴,但是界面还是非常不错。我们可以使用它快速地构建我们想要的测试界面。

在进行下面的代码之前,建议大家先阅读我之前的文章 “Elasticsearch:在 Elastic 中玩转 DeepSeek R1 来实现 RAG 应用”。在那篇文章中,我们详细地描述了如何使用 DeepSeek R1 来帮我们实现 RAG 应用。在今天的展示中,我使用 Elastic Stack 9.1.2 来展示。

alice_gradio.py 

## Install the required packages
## pip install -qU elasticsearch openai
import os
from dotenv import load_dotenv
from elasticsearch import Elasticsearch
from openai import OpenAI
import gradio as gr
import subprocessload_dotenv()ELASTICSEARCH_URL = os.getenv('ELASTICSEARCH_URL')
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ES_API_KEY = os.getenv("ES_API_KEY")
DEEPSEEK_URL = os.getenv("DEEPSEEK_URL")es_client = Elasticsearch(ELASTICSEARCH_URL,ca_certs="./http_ca.crt",api_key=ES_API_KEY,verify_certs = True
)# resp = es_client.info()
# print(resp)try:openai_client = OpenAI(api_key=OPENAI_API_KEY,base_url=DEEPSEEK_URL)
except:print("Something is wrong")index_source_fields = {"book_alice": ["content"]
}def get_elasticsearch_results(query):es_query = {"retriever": {"standard": {"query": {"semantic": {"field": "content","query": query}}}},"highlight": {"fields": {"content": {"type": "semantic","number_of_fragments": 2,"order": "score"}}},"size": 3}result = es_client.search(index="book_alice", body=es_query)return result["hits"]["hits"]def create_openai_prompt(results):context = ""for hit in results:## For semantic_text matches, we need to extract the text from the highlighted fieldif "highlight" in hit:highlighted_texts = []for values in hit["highlight"].values():highlighted_texts.extend(values)context += "\n --- \n".join(highlighted_texts)else:context_fields = index_source_fields.get(hit["_index"])for source_field in context_fields:hit_context = hit["_source"][source_field]if hit_context:context += f"{source_field}: {hit_context}\n"prompt = f"""Instructions:- You are an assistant for question-answering tasks using relevant text passages from the book Alice in wonderland- Answer questions truthfully and factually using only the context presented.- If you don't know the answer, just say that you don't know, don't make up an answer.- You must always cite the document where the answer was extracted using inline academic citation style [], using the position.- Use markdown format for code examples.- You are correct, factual, precise, and reliable.Context:{context}"""return promptdef generate_openai_completion(user_prompt, question, official):response = openai_client.chat.completions.create(model='deepseek-chat',messages=[{"role": "system", "content": user_prompt},{"role": "user", "content": question},],stream=False)return response.choices[0].message.contentdef rag_interface(query):elasticsearch_results = get_elasticsearch_results(query)context_prompt = create_openai_prompt(elasticsearch_results)answer = generate_openai_completion(context_prompt, query, official=True)return answerdemo = gr.Interface(fn=rag_interface,inputs=gr.Textbox(label="输入你的问题"),# outputs=gr.Markdown(label="RAG Answer"),outputs=gr.Textbox(label="RAG Answer"),title="Alice in Wonderland RAG QA",description="Ask a question about Alice in Wonderland and get an answer based on retrieved passages."
)demo.launch()# if __name__ == "__main__":
#     # question = "Who was at the tea party?"
#     question = "哪些人在茶会?"
#     print("Question is: ", question, "\n")#     elasticsearch_results = get_elasticsearch_results(question)
#     context_prompt = create_openai_prompt(elasticsearch_results)#     openai_completion = generate_openai_completion(context_prompt, question, official=True)
#     print(openai_completion)

这里的代码是从 Playground 里下载而来。我做了一下改动。为了能够使得我们每次都能输入我们想要的查询而不用重新运行代码,我添加了如下的代码:

def rag_interface(query):elasticsearch_results = get_elasticsearch_results(query)context_prompt = create_openai_prompt(elasticsearch_results)answer = generate_openai_completion(context_prompt, query, official=True)return answerdemo = gr.Interface(fn=rag_interface,inputs=gr.Textbox(label="输入你的问题"),# outputs=gr.Markdown(label="RAG Answer"),outputs=gr.Textbox(label="RAG Answer"),title="Alice in Wonderland RAG QA",description="Ask a question about Alice in Wonderland and get an answer based on retrieved passages."
)

就是这几行代码。它能帮我构建我们想要的界面。我们运行上面的代码:

python alice_gradio.py 
$ python alice_gradio.py 
* Running on local URL:  http://127.0.0.1:7860
* To create a public link, set `share=True` in `launch()`.

如上所示,我们打开页面 http://127.0.0.1:7860

哪些人在茶会上?

我们也可以用英文进行提问:

who were at the tea party?

http://www.dtcms.com/a/332405.html

相关文章:

  • 敏捷数据开发实践:基于 Amazon Q Developer + Remote MCP 构建本地与云端 Amazon Redshift 交互体系
  • 软件重构的破与立:模式方法创新设计与工程实践
  • 【Vibe Coding 工程之 StockAnalyzerPro 记录】- EP1.先写 PRD
  • 集成电路学习:什么是Object Detection目标检测
  • 【算法专题训练】13、回文字符串
  • 另类的pdb恢复方式
  • 逆向练习(六)Andrénalin.3/4
  • Linux应用软件编程---多任务(进程2)(资源回收函数(wait、waitpid)、exec函数族、linux下的命令、const四种位置表示的含义)
  • 一周学会Matplotlib3 Python 数据可视化-绘制树形图
  • Laravel 中解决分表问题
  • ESP32-C3_SMARTCAR
  • 高并发场景下限流算法对比与实践指南
  • 【unity实战】Unity游戏开发:如何用ScriptableObject与序列化多态实现可复用的模块化效果系统?
  • ABP vNext+ WebRTC DataChannel 低延迟传感推送
  • 物联网(IoT)系统中,通信协议如何选择
  • C++——分布式
  • Al大模型-本地私有化部署大模型-大模型微调
  • 图像识别控制技术(Sikuli)深度解析:原理、应用与商业化前景
  • Zabbix【部署 01】Zabbix企业级分布式监控系统部署配置使用实例(在线安装及问题处理)程序安装+数据库初始+前端配置+服务启动+Web登录
  • 後端開發Python篇
  • StarRocks集群部署
  • 从 0 到 1 玩转Claude code(蓝耘UI界面版本):AI 编程助手的服务器部署与实战指南
  • Xget:为您的开发工作流解锁极致速度
  • 清除 pnpm 缓存,解决不同源安装依赖包失败的问题
  • “大模型”技术专栏 | 浅谈基于 Kubernetes 的 LLM 分布式推理框架架构:概览
  • 力扣 hot100 Day74
  • Floyd 判圈算法(龟兔赛跑算法)
  • LeetCode热题100--146.LRU缓存--中等
  • SSL和TLS协议的消息认证码(MAC)
  • Grafana 与 InfluxDB 可视化深度集成(一)