กราฟส่วนบุคคลเป็นห้องสมุด Python สำหรับการสร้างการจัดการและการสืบค้นกราฟความรู้ มันมีจุดมุ่งหมายเพื่อช่วยแก้ปัญหาความท้าทายในการทำงานและหน่วยความจำระยะยาวในระบบ AI โดยเฉพาะแบบจำลองภาษาขนาดใหญ่ (LLMs)
ติดตั้งกราฟส่วนบุคคลโดยใช้ PIP:
pip install personal-graph from personal_graph import GraphDB
from personal_graph . text import text_to_graph
from personal_graph . vector_store import VliteVSS
vector_store = VliteVSS ( collection = "memories" )
graph = GraphDB ( vector_store = vector_store )
# Insert information into the graph
g = text_to_graph ( "Alice is Bob's sister. Bob works at Google." )
graph . insert_graph ( g )
# Retrieve relevant information from the graph
query = "Who is Alice?"
results = graph . search ( query )
print ( results )
# Use the retrieved information to answer questions
print ( f"Question: { query } " )
print ( f"Answer: Alice is Bob's sister." )
query = "Where does Bob work?"
results = graph . search ( query )
print ( results )
print ( f"Question: { query } " )
print ( f"Answer: Bob works at Google." )ในตัวอย่างนี้เราแทรกข้อมูลเกี่ยวกับอลิซและบ๊อบลงในกราฟความรู้ จากนั้นเราใช้วิธีการค้นหาเพื่อดึงข้อมูลที่เกี่ยวข้องตามแบบสอบถามที่กำหนด ข้อมูลที่ดึงมาสามารถใช้เป็นส่วนหนึ่งของหน่วยความจำการทำงานของ AI เพื่อตอบคำถามและให้บริบทสำหรับการโต้ตอบเพิ่มเติม
from personal_graph import GraphDB
from personal_graph . vector_store import VliteVSS
vector_store = VliteVSS ( collection = "memories" )
graph = GraphDB ( vector_store = vector_store )
# Insert information about conversations with the user over time
graph . insert (
text = "User talked about their childhood dreams and aspirations." ,
attributes = {
"date" : "2023-01-15" ,
"topic" : "childhood dreams" ,
"depth_score" : 3
})
graph . insert (
text = "User discussed their fears and insecurities in their current relationship." ,
attributes = {
"date" : "2023-02-28" ,
"topic" : "relationship fears" ,
"depth_score" : 4
})
graph . insert (
text = "User shared their spiritual beliefs and existential questions." ,
attributes = {
"date" : "2023-03-10" ,
"topic" : "spirituality and existence" ,
"depth_score" : 5
})
graph . insert (
text = "User mentioned their favorite hobbies and weekend activities." ,
attributes = {
"date" : "2023-04-02" ,
"topic" : "hobbies" ,
"depth_score" : 2
})
# User queries about the deepest conversation
query = "What was the deepest conversation we've ever had?"
deepest_conversation = graph . search ( query , sort_by = "depth_score" , descending = True , limit = 1 )ในตัวอย่างนี้เราจัดเก็บข้อมูลเกี่ยวกับการสนทนากับผู้ใช้รวมถึงวันที่หัวข้อและคะแนนความลึก คะแนนความลึกแสดงถึงความหมายของการสนทนา
เมื่อผู้ใช้ถามเกี่ยวกับการสนทนาที่ลึกที่สุดเราจะค้นหาการสนทนาด้วยคะแนนความลึกสูงสุดโดยใช้วิธีการค้นหา เราเรียงลำดับผลลัพธ์ด้วยคะแนนความลึกตามลำดับจากมากไปน้อยและ จำกัด เอาต์พุตไว้ที่การสนทนาหนึ่งครั้ง
หากพบการสนทนา AI จะตอบกลับด้วยวันที่และหัวข้อของการสนทนาที่ลึกที่สุด หากไม่พบการสนทนา AI จะแจ้งให้ผู้ใช้ทราบว่าไม่มีข้อมูลเพียงพอ
ตัวอย่างนี้แสดงให้เห็นว่ากราฟส่วนบุคคลสามารถใช้ในการสร้างหน่วยความจำระยะยาวเกี่ยวกับการโต้ตอบของผู้ใช้และดึงข้อมูลเฉพาะตามเกณฑ์เช่นความลึกการสนทนา
from personal_graph import GraphDB
from personal_graph . text import text_to_graph
from personal_graph . vector_store import VliteVSS
vector_store = VliteVSS ( collection = "memories" )
graphdb = GraphDB ( vector_store = vector_store )
nl_query = "Increased thirst, weight loss, increased hunger, and frequent urination are all symptoms of diabetes."
kg = text_to_graph ( text = nl_query )
graphdb . insert_graph ( kg )
search_query = "I am losing weight too frequently."
g = text_to_graph ( search_query )
print ( g )
graphdb . insert_graph ( g ) import os
import dspy
from personal_graph import GraphDB , PersonalRM
db = GraphDB () # storage_db is in-memory sqlite, vector_db is in vlite
turbo = dspy . OpenAI ( api_key = os . getenv ( "OPENAI_API_KEY" ))
retriever = PersonalRM ( graph = db , k = 2 )
dspy . settings . configure ( lm = turbo , rm = retriever )
class GenerateAnswer ( dspy . Signature ):
"""Answer questions with short factoid answers."""
context = dspy . InputField ( desc = "may contain relevant facts from user's graph" )
question = dspy . InputField ()
answer = dspy . OutputField (
desc = "a short answer to the question, deduced from the information found in the user's graph"
)
class RAG ( dspy . Module ):
def __init__ ( self , depth = 3 ):
super (). __init__ ()
self . retrieve = dspy . Retrieve ( k = depth )
self . generate_answer = dspy . ChainOfThought ( GenerateAnswer )
def forward ( self , question ):
context = self . retrieve ( question ). passages
prediction = self . generate_answer ( context = context , question = question )
return dspy . Prediction ( context = context , answer = prediction . answer )
rag = RAG ( depth = 2 )
response = rag ( "How is Jack related to James?" )
print ( response . answer ) from personal_graph . graph import GraphDB
from personal_graph . graph_generator import OllamaTextToGraphParser
from personal_graph . database import SQLite
from personal_graph . vector_store import VliteVSS
from personal_graph . clients import OllamaClient , OllamaEmbeddingClient
phi3 = OllamaClient ( model_name = "phi3" )
nomic_embed = OllamaEmbeddingClient ( model_name = "nomic-embed-text" )
storage_db = SQLite ( local_path = "./local.db" )
vector_store = VliteVSS ( collection = "./vectors" )
graph_generator = OllamaTextToGraphParser ( llm_client = phi3 )
print ( graph_generator ) # Should print the InstructorGraphGenerator
with GraphDB (
database = storage_db ,
vector_store = vector_store ,
graph_generator = graph_generator
) as db :
print ( db )ต่อไปนี้เป็นเพียงภาพร่างของการไหลที่วางแผนไว้ WIP
graphdb = GraphDB ( storage = db , vector_store = vector_store , graph_generator = graph_generator )
graphdb . load_dataset ( "KarateClub" )
pyg_graph = graphdb . to_pyg ()
updated_graph = model ( pyg_graph ) # Run Neural Network algorithms here using PyG
graphdb . from_pyg ( updated_graph )วิดีโอนี้อธิบายถึงไลบรารีกราฟส่วนบุคคลได้ดีที่สุด [! กราฟส่วนบุคคล]
สำหรับรายละเอียดเพิ่มเติมและเอกสาร API ดูเอกสารประกอบกราฟส่วนบุคคล
ยินดีต้อนรับ! อย่าลังเลที่จะสร้างปัญหาสำหรับข้อบกพร่องและคำขอคุณสมบัติ
กราฟส่วนบุคคลได้รับการปล่อยตัวภายใต้ใบอนุญาต MIT
คำถามข้อเสนอแนะหรือข้อเสนอแนะ? ติดต่อที่ [email protected] หรือเปิดปัญหาเกี่ยวกับ GitHub