中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

wordpress contactusseo文章

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

昨天寫了一篇文章,使用fastapi直接操作neo4j圖數(shù)據(jù)庫插入數(shù)據(jù)的例子, 本文實現(xiàn)LLM大模型結(jié)合neo4j圖數(shù)據(jù)庫實現(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)建一個基于FastAPI和Gradio的聊天應(yīng)用,主要功能如下:

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

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

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

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

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

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

  1. 自然語言處理:

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

  1. Cypher 查詢生成:

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

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

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

  1. 結(jié)果解釋:

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

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

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

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

在這里插入圖片描述

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

在這里插入圖片描述

http://www.risenshineclean.com/news/34302.html

相關(guān)文章:

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