国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

    當(dāng)前位置: 首頁 > news >正文

    wordpress contactusseo文章

    wordpress contactus,seo文章,做電腦網(wǎng)站步驟,做網(wǎng)站銀川昨天寫了一篇文章,使用fastapi直接操作neo4j圖數(shù)據(jù)庫插入數(shù)據(jù)的例子, 本文實(shí)現(xiàn)LLM大模型結(jié)合neo4j圖數(shù)據(jù)庫實(shí)現(xiàn)AI問答功能。 廢話不多說,先上代碼 import gradio as gr from fastapi import FastAPI, HTTPException, Request from pydantic…

    昨天寫了一篇文章,使用fastapi直接操作neo4j圖數(shù)據(jù)庫插入數(shù)據(jù)的例子, 本文實(shí)現(xiàn)LLM大模型結(jié)合neo4j圖數(shù)據(jù)庫實(shí)現(xiàn)AI問答功能。

    廢話不多說,先上代碼

    import gradio as gr
    from fastapi import FastAPI, HTTPException, Request
    from pydantic import BaseModel
    from langchain_openai import ChatOpenAI
    from langchain.prompts import (ChatPromptTemplate,MessagesPlaceholder,SystemMessagePromptTemplate,HumanMessagePromptTemplate,
    )
    from langchain.chains import LLMChain
    from langchain.memory import ConversationBufferMemory
    from langchain_community.graphs import Neo4jGraph
    from langchain.chains import GraphCypherQAChain
    import asyncio
    from typing import List
    import json# Initialize FastAPI
    app = FastAPI()# Initialize Neo4j with timeout
    try:graph = Neo4jGraph(url="bolt://localhost:7687",username="neo4j",password="password",database="neo4j",timeout=60  # 60 seconds timeout)
    except Exception as e:print(f"Failed to connect to Neo4j: {e}")graph = None# Fallback in-memory storage
    job_seekers = []
    job_positions = []# Initialize LangChain components
    llm = ChatOpenAI(temperature=0.95,model="glm-4-flash",openai_api_key="xxxxxx",openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
    )prompt = ChatPromptTemplate(messages=[SystemMessagePromptTemplate.from_template("You are a helpful AI assistant for a recruitment company. You can answer questions about job seekers and available positions."),MessagesPlaceholder(variable_name="chat_history"),HumanMessagePromptTemplate.from_template("{question}")]
    )memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
    conversation = LLMChain(llm=llm,prompt=prompt,verbose=True,memory=memory
    )# Initialize GraphCypherQAChain if Neo4j is available
    if graph:graph_qa = GraphCypherQAChain.from_llm(llm,graph=graph,verbose=True)# Define chat function with timeout
    async def chat_with_timeout(message, history):try:if graph:neo4j_response = await asyncio.wait_for(asyncio.to_thread(graph_qa.run, message),timeout=10.0  # 10 seconds timeout)return f"Based on our database: {neo4j_response}"else:# Fallback to in-memory dataif "job seekers" in message.lower():return f"Based on our records: We have {len(job_seekers)} job seekers."elif "job positions" in message.lower():return f"Based on our records: We have {len(job_positions)} job positions."else:response = conversation.invoke({"question": message})return response['text']except asyncio.TimeoutError:return "I'm sorry, but the database query took too long. Please try a simpler question or try again later."except Exception as e:print(f"Error in chat function: {e}")response = conversation.invoke({"question": message})return response['text']# # Create Gradio interface
    iface = gr.ChatInterface(chat_with_timeout)
    #
    # # Mount Gradio app to FastAPI
    app = gr.mount_gradio_app(app, iface, path="/")# Run the app
    if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
    

    還是老規(guī)矩,先AI解釋下,構(gòu)建一個(gè)基于FastAPI和Gradio的聊天應(yīng)用,主要功能如下:

    1、初始化FastAPI應(yīng)用和Neo4j圖數(shù)據(jù)庫連接(帶超時(shí)處理);

    2、定義了用于對(duì)話的LangChain組件,包括LLM模型、提示模板及對(duì)話記憶;

    3、根據(jù)Neo4j是否可用初始化圖查詢鏈;

    4、實(shí)現(xiàn)異步聊天函數(shù),支持?jǐn)?shù)據(jù)庫查詢數(shù)據(jù)檢索,并處理超時(shí)錯(cuò)誤;

    5、使用Gradio創(chuàng)建用戶界面并將應(yīng)用掛載到FastAPI上。

    核心關(guān)注graph_qa.run方法,執(zhí)行原理:

    1. 自然語言處理:

    當(dāng)調(diào)用 graph_qa.run(message) 時(shí),首先會(huì)將用戶的自然語言查詢(message)傳遞給大語言模型(LLM)。

    1. Cypher 查詢生成:

    LLM 分析用戶的查詢,并嘗試將其轉(zhuǎn)換為 Cypher 查詢語言。Cypher 是 Neo4j 圖數(shù)據(jù)庫使用的查詢語言。這個(gè)步驟涉及到理解用戶意圖和將其映射到圖數(shù)據(jù)庫的結(jié)構(gòu)上。

    1. 數(shù)據(jù)庫查詢:

    生成的 Cypher 查詢被發(fā)送到 Neo4j 數(shù)據(jù)庫執(zhí)行。這個(gè)過程涉及到遍歷圖數(shù)據(jù)庫,匹配節(jié)點(diǎn)和關(guān)系,并檢索相關(guān)數(shù)據(jù)。

    1. 結(jié)果解釋:

    數(shù)據(jù)庫返回查詢結(jié)果后,這些結(jié)果會(huì)被傳回給 LLM。LLM 會(huì)分析這些原始數(shù)據(jù),理解其含義和上下文。

    1. 響應(yīng)生成:

    最后,LLM 會(huì)根據(jù)原始查詢和數(shù)據(jù)庫返回的結(jié)果,生成一個(gè)人類可讀的響應(yīng)。這個(gè)響應(yīng)應(yīng)該直接回答用戶的問題,并可能包含從數(shù)據(jù)庫中提取的具體信息。

    在上一篇文章中,我已經(jīng)在neo4j插入了一些數(shù)據(jù),比如張三1的技能。 這里問一下

    在這里插入圖片描述

    原文鏈接: 【知識(shí)圖譜】4、LLM大模型結(jié)合neo4j圖數(shù)據(jù)庫實(shí)現(xiàn)AI問答的功能

    在這里插入圖片描述

    http://aloenet.com.cn/news/34302.html

    相關(guān)文章:

  1. 新手php網(wǎng)站建設(shè)微博上如何做網(wǎng)站推廣
  2. 網(wǎng)站建設(shè)設(shè)計(jì)制作方案與價(jià)格seo資訊
  3. 網(wǎng)站建設(shè)活動(dòng)計(jì)劃seo軟件排行榜前十名
  4. 網(wǎng)站備案 網(wǎng)址營銷助手
  5. 冠縣做網(wǎng)站推廣3d建模培訓(xùn)學(xué)校哪家好
  6. 企業(yè)網(wǎng)站建設(shè)合同書標(biāo)準(zhǔn)版湖南疫情最新情況
  7. 計(jì)算機(jī)科學(xué)專業(yè)就業(yè)方向石家莊seo報(bào)價(jià)
  8. 網(wǎng)站備案是 備案空間嗎考試培訓(xùn)
  9. 怎樣做自己的銷售網(wǎng)站6草根seo視頻大全網(wǎng)站
  10. 機(jī)械設(shè)備網(wǎng)優(yōu)化內(nèi)容
  11. 網(wǎng)站建設(shè)方案及報(bào)價(jià)單seo外包優(yōu)化網(wǎng)站
  12. 服務(wù)器怎么發(fā)布網(wǎng)站國際新聞最新消息十條
  13. php動(dòng)態(tài)網(wǎng)站開發(fā)實(shí)例教程第2版域名查詢138ip
  14. 怎樣做電商網(wǎng)站社群營銷案例
  15. 法人變更在哪個(gè)網(wǎng)站做公示重慶森林為什么不能看
  16. 知名的網(wǎng)站制作武漢網(wǎng)絡(luò)推廣優(yōu)化
  17. bazien wordpress旅游企業(yè)seo官網(wǎng)分析報(bào)告
  18. php商城網(wǎng)站建設(shè)多少錢百度推廣營銷怎么做
  19. 織夢整形醫(yī)院網(wǎng)站開發(fā)江門網(wǎng)站優(yōu)化公司
  20. 駕校網(wǎng)站建設(shè)關(guān)鍵詞北京網(wǎng)站優(yōu)化哪家好
  21. java做網(wǎng)站與php做網(wǎng)站鏈接提交
  22. 開個(gè)網(wǎng)站做上海關(guān)鍵詞優(yōu)化推薦
  23. 知名網(wǎng)站建設(shè)查排名官網(wǎng)
  24. 延吉網(wǎng)站優(yōu)化網(wǎng)絡(luò)營銷的策略包括
  25. 怎么樣做網(wǎng)站的目錄結(jié)構(gòu)查找網(wǎng)站
  26. 麗江網(wǎng)絡(luò)推廣廊坊seo推廣公司
  27. 今天天津最新通告南寧seo優(yōu)化
  28. 怎樣建設(shè)公司網(wǎng)站小程序seo服務(wù)商排名
  29. 網(wǎng)站建設(shè)項(xiàng)目報(bào)價(jià)網(wǎng)站歷史權(quán)重查詢
  30. 網(wǎng)站改版 百度北京seo優(yōu)化技術(shù)