expand + knowledge graph
Browse files- knowledge.py +34 -5
knowledge.py
CHANGED
|
@@ -31,12 +31,13 @@ class KnowledgeGraph(BaseModel):
|
|
| 31 |
|
| 32 |
from groq import Groq
|
| 33 |
import os
|
| 34 |
-
|
| 35 |
# Initialize with API key
|
| 36 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 37 |
|
| 38 |
# Enable instructor patches for Groq client
|
| 39 |
client = instructor.from_groq(client)
|
|
|
|
| 40 |
"""
|
| 41 |
from openai import OpenAI
|
| 42 |
client = instructor.from_openai(
|
|
@@ -47,18 +48,46 @@ client = instructor.from_openai(
|
|
| 47 |
mode=instructor.Mode.JSON,
|
| 48 |
)
|
| 49 |
"""
|
| 50 |
-
def generate_graph(input) -> KnowledgeGraph:
|
| 51 |
return client.chat.completions.create(
|
| 52 |
-
model=
|
| 53 |
max_retries=5,
|
| 54 |
messages=[
|
| 55 |
{
|
| 56 |
"role": "user",
|
| 57 |
-
|
| 58 |
}
|
| 59 |
],
|
| 60 |
response_model=KnowledgeGraph,
|
| 61 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def graph(query):
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
return graph.json()
|
|
|
|
| 31 |
|
| 32 |
from groq import Groq
|
| 33 |
import os
|
| 34 |
+
|
| 35 |
# Initialize with API key
|
| 36 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 37 |
|
| 38 |
# Enable instructor patches for Groq client
|
| 39 |
client = instructor.from_groq(client)
|
| 40 |
+
llm='llama-3.1-8b-instant' #"llama3.2", #
|
| 41 |
"""
|
| 42 |
from openai import OpenAI
|
| 43 |
client = instructor.from_openai(
|
|
|
|
| 48 |
mode=instructor.Mode.JSON,
|
| 49 |
)
|
| 50 |
"""
|
| 51 |
+
def generate_graph(q, input) -> KnowledgeGraph:
|
| 52 |
return client.chat.completions.create(
|
| 53 |
+
model=llm,
|
| 54 |
max_retries=5,
|
| 55 |
messages=[
|
| 56 |
{
|
| 57 |
"role": "user",
|
| 58 |
+
"content": f"Help me understand the following by describing it as a detailed knowledge graph: ### Question: {q} ### Context: {input}",
|
| 59 |
}
|
| 60 |
],
|
| 61 |
response_model=KnowledgeGraph,
|
| 62 |
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class Issue(BaseModel):
|
| 66 |
+
"Break down Issue as sub issues"
|
| 67 |
+
question: str
|
| 68 |
+
|
| 69 |
+
class IssuePlan(BaseModel):
|
| 70 |
+
"List of Issue"
|
| 71 |
+
issue_graph: List[Issue] = []
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def expandIssue(input) -> IssuePlan:
|
| 75 |
+
return client.chat.completions.create(
|
| 76 |
+
model=llm,
|
| 77 |
+
max_retries=10,
|
| 78 |
+
messages=[
|
| 79 |
+
{
|
| 80 |
+
"role": "system",
|
| 81 |
+
"content": "As a Mckinsey Consultant create a framework that relevant to the topic, list all issues.",
|
| 82 |
+
}, {
|
| 83 |
+
"role": "user",
|
| 84 |
+
"content": f"Question: {input}",
|
| 85 |
+
},
|
| 86 |
+
],
|
| 87 |
+
response_model=IssuePlan,
|
| 88 |
+
)
|
| 89 |
def graph(query):
|
| 90 |
+
queryx = expandIssue(query)
|
| 91 |
+
ctx = ", ".join([q.question for q in queryx.issue_graph])
|
| 92 |
+
graph = generate_graph(query, ctx)
|
| 93 |
return graph.json()
|