import instructor from pydantic import BaseModel, Field from typing import List from graphviz import Digraph class Node(BaseModel, frozen=True): """ Node representing concept in the subject domain """ id: int label: str = Field(..., description = "description of the concept in the subject domain") color: str class Edge(BaseModel, frozen=True): """ Edge representing relationship between concepts in the subject domain """ source: int = Field(..., description = "source representing concept in the subject domain") target: int = Field(..., description = "target representing concept in the subject domain") label: str = Field(..., description = "description representing relationship between concepts in the subject domain") color: str = "black" class KnowledgeGraph(BaseModel): """ graph representation of concepts in the subject domain """ nodes: List[Node] = Field(..., default_factory=list) edges: List[Edge] = Field(..., default_factory=list) from groq import Groq import os # Initialize with API key client = Groq(api_key=os.getenv("GROQ_API_KEY")) # Enable instructor patches for Groq client client = instructor.from_groq(client) llm='llama-3.1-8b-instant' #"llama3.2", # """ from openai import OpenAI client = instructor.from_openai( OpenAI( base_url="http://localhost:11434/v1", api_key="ollama", ), mode=instructor.Mode.JSON, ) """ def generate_graph(q, input) -> KnowledgeGraph: return client.chat.completions.create( model=llm, max_retries=5, messages=[ { "role": "user", "content": f"Help me understand the following by describing it as a detailed knowledge graph: ### Question: {q} ### Context: {input}", } ], response_model=KnowledgeGraph, ) class Issue(BaseModel): "Break down Issue as sub issues" question: str class IssuePlan(BaseModel): "List of Issue" issue_graph: List[Issue] = [] def expandIssue(input) -> IssuePlan: return client.chat.completions.create( model=llm, max_retries=10, messages=[ { "role": "system", "content": "As a Mckinsey Consultant create a framework that relevant to the topic, list all issues.", }, { "role": "user", "content": f"Question: {input}", }, ], response_model=IssuePlan, ) def graph(query): queryx = expandIssue(query) ctx = ", ".join([q.question for q in queryx.issue_graph]) graph = generate_graph(query, ctx) return graph.json()