File size: 5,389 Bytes
6d55408 6866239 6d55408 6866239 6d55408 6866239 6d55408 6866239 6d55408 6866239 6d55408 6866239 6d55408 6866239 6d55408 6866239 6d55408 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import os
from langchain_huggingface import HuggingFaceEmbeddings
from groq import Groq
from pymilvus import connections, Collection
class ChatHandler:
def __init__(self,api_token,grok_api_token,logger):
self.logger = logger
self.logger.info("Initializing ChatHandler...")
self.groq_client = Groq(api_key=grok_api_token)
# Initialize the embedding model using Hugging Face
self.embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={"token": api_token},
)
def _query_groq_model(self, prompt):
"""
Query Groq's Llama model using the SDK.
"""
try:
chat_completion = self.groq_client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.1-8b-instant", # Ensure the model name is correct
)
# Return the assistant's response
return chat_completion.choices[0].message.content
except Exception as e:
self.logger.error(f"Error querying Groq API: {e}")
return f"Error querying Groq API: {e}"
def answer_question(self, question):
# Generate embedding for the question
self.logger.info(f"Received question: {question}")
collections = connections._fetch_handler().list_collections()
responses = []
for collection_name in collections:
collection = Collection(name=collection_name)
embeddings = self.embeddings.embed_query(question)
search_params = {
"metric_type": "IP",
"params": {"nprobe": 10},
}
results = collection.search(
data=[embeddings],
anns_field="embedding",
param=search_params,
limit=5,
)
# Extract the embeddings or metadata (if needed)
for res in results[0]:
# Store the ID or use res.distance if needed for similarity score
responses.append({"id": res.id, "distance": res.distance,"content":res.entity})
if responses:
sorted_responses = sorted(responses, key=lambda x: x["distance"], reverse=True)
prompt = self._generate_prompt(question, sorted_responses[:5])
response = self._query_groq_model(prompt)
return response
return "No relevant documents found or context is insufficient to answer your question."
def _generate_prompt(self, question, documents):
"""
Generate a structured prompt tailored to analyze government energy consumption data
and answer questions effectively using the provided documents.
"""
context = "\n".join(
[
f"Document {i + 1}:\nID: {doc['id']}\nSimilarity: {doc['distance']:.4f}\nContent: {doc['content']}"
for i, doc in enumerate(documents[:5])
]
)
prompt = f"""
You are an advanced AI assistant with expertise in 5G network optimization, deployment strategies,
and resource allocation. Your role is to analyze network datasets to identify inefficiencies,
propose actionable deployment and optimization strategies, and quantify potential improvements.
### Data Provided:
The following documents contain detailed information about 5G network deployment, resource utilization,
and operational metrics:
{context}
### Question:
{question}
### Instructions:
1. **Highlight Areas of Network Inefficiencies**:
- Identify inefficiencies such as underutilized network nodes, high latency areas, or
imbalanced resource allocation.
- Use data points from the documents to back your observations.
2. **Suggest Strategies for Network Optimization**:
- Recommend actionable steps such as adjusting network configurations, deploying additional nodes,
or reallocating bandwidth.
- Ensure suggestions are feasible and aligned with the provided datasets.
3. **Quantify Cost-Saving and Performance Benefits**:
- Provide quantitative estimates of potential cost savings from the suggested strategies.
- Highlight the performance benefits, such as improved latency, higher throughput, or enhanced user experience.
4. **Present the Response Clearly**:
- Organize your findings in a step-by-step format.
- Use tables, bullet points, or concise paragraphs for clarity.
### Example Output Format:
- **Network Inefficiencies Identified**:
1. ...
2. ...
- **Optimization Strategies**:
1. ...
2. ...
- **Cost-Saving and Performance Benefits**:
- Cost Savings: $...
- Performance Improvements: ...
Please ensure the response is data-driven, actionable, and easy to understand.
"""
return prompt
|