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

湖南张家界建设厅网站大宇网络做网站怎么样

湖南张家界建设厅网站,大宇网络做网站怎么样,公司官网的seo,石家庄建站程序一、Chainlit介绍 Chainlit是一个开源Python包#xff0c;旨在彻底改变构建和共享语言模型#xff08;LM#xff09;应用程序的方式。Chainlit可以创建用户界面#xff08;UI#xff09;#xff0c;类似于由OpenAI开发的ChatGPT用户界面#xff0c;Chainlit可以开发类似… 一、Chainlit介绍 Chainlit是一个开源Python包旨在彻底改变构建和共享语言模型LM应用程序的方式。Chainlit可以创建用户界面UI类似于由OpenAI开发的ChatGPT用户界面Chainlit可以开发类似streamlit的web界面。 1.1 Chainlit的主要特点 可视化中间步骤Chainlit可以可视化大语言模型管道中的每个步骤Chainlit与Python代码轻松集成可以快速释放LM应用程序的潜力快速响应的UI开发使用Chainlit可以利用其直观的框架来设计和实现类似于ChatGPT的迷人UI。 1.2 Chainlit装饰器功能 on_message 与框架的装饰器用于对来自UI的消息作出反应。每次收到新消息时都会调用装饰函数。 on_chat_start Decorator对用户websocket连接事件作出反应。 1.3 概念 User Session user_session是一个存储用户会话数据的字典id和env键分别保持会话id和环境变量。用户会话其他数据存储在其他key中。 Streaming Chainlit支持两种类型的流 Python Streaminghttps://docs.chainlit.io/concepts/streaming/python Langchain Streaminghttps://docs.chainlit.io/concepts/streaming/langchain 二、实施步骤 1.开始上传PDF格式文件确保其正确提交 2.随后使用PyPDF2从上传的PDF文档中提取文本内容 3.利用OpenAIEmbeddings将提取的文本内容转换为矢量化嵌入 4.将这些矢量化嵌入保存在指定的向量库中比如Chromadb 5.当用户查询时通过应用OpenAIEmbeddings将查询转换为相应的矢量嵌入将查询的语义结构对齐到矢量化域中 6.调用查询的矢量化嵌入有效地检索上下文相关的文档和文档上下文的相关元数据 7.将检索到的相关文档及其附带的元数据传递给LLM从而生成响应。 三、代码实施 3.1 安装所需的包 pip install -qU langchain openai tiktoken pyPDF2 chainlitconda install -c conda-forge chromadb 3.2 代码实施 #import required librariesfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.vectorstores import Chromafrom langchain.chains import RetrievalQAWithSourcesChainfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts.chat import (ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate)#import chainlit as climport PyPDF2from io import BytesIOfrom getpass import getpass#import osfrom configparser import ConfigParserenv_config ConfigParser()​# Retrieve the openai key from the environmental variablesdef read_config(parser: ConfigParser, location: str) - None: assert parser.read(location), fCould not read config {location}#CONFIG_FILE os.path.join(./env, env.conf)read_config(env_config, CONFIG_FILE)api_key env_config.get(openai, api_key).strip()​#os.environ[OPENAI_API_KEY] api_key​# Chunking the texttext_splitter RecursiveCharacterTextSplitter(chunk_size1000,chunk_overlap100)##system templatesystem_template Use the following pieces of context to answer the users question.If you dont know the answer, just say that you dont know, dont try to make up an answer.ALWAYS return a SOURCES part in your answer.The SOURCES part should be a reference to the source of the document from which you got your answer.​​​Begin!----------------{summaries}messages [SystemMessagePromptTemplate.from_template(system_template),HumanMessagePromptTemplate.from_template({question}),]prompt ChatPromptTemplate.from_messages(messages)chain_type_kwargs {prompt: prompt}​#Decorator to react to the user websocket connection event. cl.on_chat_startasync def init(): files None​ # Wait for the user to upload a PDF file while files is None: files await cl.AskFileMessage( contentPlease upload a PDF file to begin!, accept[application/pdf], ).send()​ file files[0]​ msg cl.Message(contentfProcessing {file.name}...) await msg.send()​ # Read the PDF file pdf_stream BytesIO(file.content) pdf PyPDF2.PdfReader(pdf_stream) pdf_text for page in pdf.pages: pdf_text page.extract_text()​ # Split the text into chunks texts text_splitter.split_text(pdf_text)​ # Create metadata for each chunk metadatas [{source: f{i}-pl} for i in range(len(texts))]​ # Create a Chroma vector store embeddings OpenAIEmbeddings(openai_api_keyos.getenv(OPENAI_API_KEY)) docsearch await cl.make_async(Chroma.from_texts)( texts, embeddings, metadatasmetadatas )​ # Create a chain that uses the Chroma vector store chain RetrievalQAWithSourcesChain.from_chain_type( ChatOpenAI(temperature0, openai_api_keyos.environ[OPENAI_API_KEY]), chain_typestuff, retrieverdocsearch.as_retriever(), )​ # Save the metadata and texts in the user session cl.user_session.set(metadatas, metadatas) cl.user_session.set(texts, texts)​ # Let the user know that the system is ready msg.content f{file.name} processed. You can now ask questions! await msg.update()​ cl.user_session.set(chain, chain)​# react to messages coming from the UIcl.on_messageasync def process_response(res): chain cl.user_session.get(chain) # type: RetrievalQAWithSourcesChain cb cl.AsyncLangchainCallbackHandler( stream_final_answerTrue, answer_prefix_tokens[FINAL, ANSWER]) cb.answer_reached True res await chain.acall(res, callbacks[cb]) print(fresponse: {res}) answer res[answer] sources res[sources].strip() source_elements []​ # Get the metadata and texts from the user session metadatas cl.user_session.get(metadatas) all_sources [m[source] for m in metadatas] texts cl.user_session.get(texts)​ if sources: found_sources []​ # Add the sources to the message for source in sources.split(,): source_name source.strip().replace(., ) # Get the index of the source try: index all_sources.index(source_name) except ValueError: continue text texts[index] found_sources.append(source_name) # Create the text element referenced in the message source_elements.append(cl.Text(contenttext, namesource_name))​ if found_sources: answer f\nSources: {, .join(found_sources)} else: answer \nNo sources found​ if cb.has_streamed_final_answer: cb.final_stream.elements source_elements await cb.final_stream.update() else: await cl.Message(contentanswer, elementssource_elements).send() 3.3 运行应用程序 chainlit run name of the python script 3.4 Chainlit UI 点击返回的页码详细说明所引用的文档内容。 我们也可以更改设置。 参考文献 [1] https://medium.aiplanet.com/building-llm-application-for-document-question-answering-using-chainlit-d15d10469069
http://www.pierceye.com/news/396196/

相关文章:

  • 哈尔滨做网站的价格如何利用wordpress搭建一个发卡网
  • 商会建设网站说明网站建设属于技术活吗
  • 免费申请手机网站公司画册模板免费下载
  • 网站建设策划做一个卖货的app要多少钱
  • 泉州网站平台建设公司网站服务器出错了怎么办
  • 佛山网站设计专业手机网站模板设计软件
  • 顺德网站优化公司wordpress 去广告
  • 自己建企业网站怎么建免费大数据查询
  • 短视频素材免费下载网站江西省城乡建设厅网站
  • 西安做网站公司工资wordpress 导航 分类
  • 网站建设免费教程网页设计图片高清
  • 网站后台源代码东直门小学的网站建设
  • 无锡建设局评职称网站分析 网站
  • 宜昌需要做网站建设的公司python做调查问卷网站
  • 乐陵森大最好的seo优化公司
  • php网站开发实施方案福建建筑信息平台
  • 怎么写公司网站的文案建设网站技术公司简介
  • 陕西做网站的wordpress增加变量
  • 莒南县建设工程网站网站认证方式有几种
  • 举报网站建设自查报告相同网站名
  • 网站建设网站建设公司北京logo设计制作
  • 福州市交通建设集团有限公司 网站全国国家公示系统官网
  • 郑州网站建设蝶动2023年专升本最新政策
  • 江苏网站推广网络企业宣传画册设计公司
  • 沈阳设计网站公司哪家好个人养老保险怎么交费
  • 烟台seo网站推广如何给自己公司设计logo
  • wordpress 图片大小设置梧州网站优化公司
  • 360推广 网站建设豫建设标去哪个网站
  • 职高网站建设知识点国家免费技能培训有哪些
  • 哪个网站有帮忙做公开课课件怎么查那些人输入做网站