Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- tools/knowledge_base_tool.py +78 -0
- tools/send_email.py +47 -0
tools/knowledge_base_tool.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from openai import AzureOpenAI
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
# Config
|
| 9 |
+
AZURE_SEARCH_ENDPOINT = os.getenv("AZURE_SEARCH_ENDPOINT")
|
| 10 |
+
AZURE_SEARCH_KEY = os.getenv("AZURE_SEARCH_KEY")
|
| 11 |
+
AZURE_SEARCH_INDEX_NAME = "it-ticket-solutions-index"
|
| 12 |
+
|
| 13 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
+
API_BASE_URL = os.getenv("API_BASE_URL")
|
| 15 |
+
MODEL_NAME = os.getenv("MODEL_NAME")
|
| 16 |
+
AZURE_OPENAI_API_VERSION = "2024-02-15-preview"
|
| 17 |
+
|
| 18 |
+
# Clients
|
| 19 |
+
openai_client = AzureOpenAI(
|
| 20 |
+
api_key=HF_TOKEN,
|
| 21 |
+
api_version=AZURE_OPENAI_API_VERSION,
|
| 22 |
+
azure_endpoint=API_BASE_URL
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def embed_text(text: str):
|
| 26 |
+
response = openai_client.embeddings.create(
|
| 27 |
+
input=[text],
|
| 28 |
+
model=MODEL_NAME
|
| 29 |
+
)
|
| 30 |
+
return response.data[0].embedding
|
| 31 |
+
|
| 32 |
+
def search_similar_solution(query: str, category: str) -> str:
|
| 33 |
+
embedding = embed_text(query)
|
| 34 |
+
|
| 35 |
+
url = f"{AZURE_SEARCH_ENDPOINT}/indexes/{AZURE_SEARCH_INDEX_NAME}/docs/search?api-version=2023-07-01-Preview"
|
| 36 |
+
|
| 37 |
+
headers = {
|
| 38 |
+
"Content-Type": "application/json",
|
| 39 |
+
"api-key": AZURE_SEARCH_KEY
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
payload = {
|
| 43 |
+
"search": "",
|
| 44 |
+
"vectors": [
|
| 45 |
+
{
|
| 46 |
+
"value": embedding,
|
| 47 |
+
"fields": "embedding",
|
| 48 |
+
"k": 3
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
"select": "category,problem,solution",
|
| 52 |
+
"filter": f"category eq '{category}'"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 56 |
+
if response.status_code != 200:
|
| 57 |
+
return f"Error while searching: {response.text}"
|
| 58 |
+
|
| 59 |
+
results = response.json().get("value", [])
|
| 60 |
+
|
| 61 |
+
if not results:
|
| 62 |
+
return "No matching solutions found."
|
| 63 |
+
|
| 64 |
+
response_text = ""
|
| 65 |
+
for idx, doc in enumerate(results, 1):
|
| 66 |
+
response_text += (
|
| 67 |
+
f"\nResult {idx}:\n"
|
| 68 |
+
f"Category: {doc.get('category')}\n"
|
| 69 |
+
f"Problem: {doc.get('problem')}\n"
|
| 70 |
+
f"Solution: {doc.get('solution')}\n"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return response_text
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
query = "My Outlook crashes every time I open it"
|
| 77 |
+
rag_output = search_similar_solution(query, category="Software Bug")
|
| 78 |
+
print(rag_output)
|
tools/send_email.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import smtplib
|
| 2 |
+
from email.mime.text import MIMEText
|
| 3 |
+
from email.mime.multipart import MIMEMultipart
|
| 4 |
+
|
| 5 |
+
# Replace these with your actual SMTP credentials
|
| 6 |
+
SMTP_SERVER = "smtp.gmail.com"
|
| 7 |
+
SMTP_PORT = 587
|
| 8 |
+
SENDER_EMAIL = "coder6514@gmail.com"
|
| 9 |
+
SENDER_PASSWORD = "jcpmwlqwiwtbnblq" # Use App Password for Gmail
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def send_email(to_email: str, subject: str, body: str) -> bool:
|
| 13 |
+
try:
|
| 14 |
+
msg = MIMEMultipart()
|
| 15 |
+
msg["From"] = SENDER_EMAIL
|
| 16 |
+
msg["To"] = to_email
|
| 17 |
+
msg["Subject"] = subject
|
| 18 |
+
|
| 19 |
+
msg.attach(MIMEText(body, "plain"))
|
| 20 |
+
|
| 21 |
+
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
| 22 |
+
server.starttls()
|
| 23 |
+
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
| 24 |
+
server.sendmail(SENDER_EMAIL, to_email, msg.as_string())
|
| 25 |
+
|
| 26 |
+
return True
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print("Error sending email:", str(e))
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
def escalate_ticket_with_email(issue: str) -> dict:
|
| 32 |
+
subject = "Escalation: Unresolved IT Issue"
|
| 33 |
+
body = f"""
|
| 34 |
+
Hello IT Support Team,
|
| 35 |
+
|
| 36 |
+
The following issue reported by a user could not be resolved by the AI Assistant:
|
| 37 |
+
|
| 38 |
+
"{issue}"
|
| 39 |
+
|
| 40 |
+
Please investigate and take further action.
|
| 41 |
+
|
| 42 |
+
Regards,
|
| 43 |
+
AI Notification Agent
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
success = send_email(to_email="coder6514@gmail.com", subject=subject, body=body)
|
| 47 |
+
return {"content": "📧 Email sent to IT support." if success else "⚠️ Failed to send email."}
|