97e91a83f3
Ruff / Ruff (push) Waiting to run
Test / Core Tests (push) Waiting to run
Test / Offline Coverage Tests (Python 3.10) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.11) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.12) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.13) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.9) (push) Waiting to run
Test / Full Coverage (Python 3.11) (push) Waiting to run
Test / Core Provider Tests (OpenAI) (push) Blocked by required conditions
Test / Core Provider Tests (Anthropic) (push) Blocked by required conditions
Test / Core Provider Tests (Google) (push) Blocked by required conditions
Test / Core Provider Tests (Other) (push) Blocked by required conditions
Test / Anthropic Tests (push) Blocked by required conditions
Test / Gemini Tests (push) Blocked by required conditions
Test / Google GenAI Tests (push) Blocked by required conditions
Test / Vertex AI Tests (push) Blocked by required conditions
Test / OpenAI Tests (push) Blocked by required conditions
Test / Writer Tests (push) Blocked by required conditions
Test / Auto Client Tests (push) Blocked by required conditions
ty / type-check (push) Waiting to run
59 lines
1.3 KiB
Python
59 lines
1.3 KiB
Python
import instructor
|
|
|
|
from graphviz import Digraph
|
|
from pydantic import BaseModel, Field
|
|
from openai import OpenAI
|
|
|
|
|
|
client = instructor.from_openai(OpenAI())
|
|
|
|
|
|
class Node(BaseModel):
|
|
id: int
|
|
label: str
|
|
color: str
|
|
|
|
|
|
class Edge(BaseModel):
|
|
source: int
|
|
target: int
|
|
label: str
|
|
color: str = "black"
|
|
|
|
|
|
class KnowledgeGraph(BaseModel):
|
|
nodes: list[Node] = Field(..., default_factory=list)
|
|
edges: list[Edge] = Field(..., default_factory=list)
|
|
|
|
|
|
def generate_graph(input) -> KnowledgeGraph:
|
|
return client.chat.completions.create(
|
|
model="gpt-3.5-turbo-16k",
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": f"Help me understand following by describing as a detailed knowledge graph: {input}",
|
|
}
|
|
],
|
|
response_model=KnowledgeGraph,
|
|
) # type: ignore
|
|
|
|
|
|
def visualize_knowledge_graph(kg: KnowledgeGraph):
|
|
dot = Digraph(comment="Knowledge Graph")
|
|
|
|
# Add nodes
|
|
for node in kg.nodes:
|
|
dot.node(str(node.id), node.label, color=node.color)
|
|
|
|
# Add edges
|
|
for edge in kg.edges:
|
|
dot.edge(str(edge.source), str(edge.target), label=edge.label, color=edge.color)
|
|
|
|
# Render the graph
|
|
dot.render("knowledge_graph.gv", view=True)
|
|
|
|
|
|
graph: KnowledgeGraph = generate_graph("Teach me about quantum mechanics")
|
|
visualize_knowledge_graph(graph)
|