TANMIHA commited on
Commit
a2f534a
·
verified ·
1 Parent(s): f2c86a2

Update tools/knowledge_base_tool.py

Browse files
Files changed (1) hide show
  1. tools/knowledge_base_tool.py +77 -77
tools/knowledge_base_tool.py CHANGED
@@ -1,78 +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)
 
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)