Spaces:
Sleeping
Sleeping
Download tools/knowledge_base_tool.py from MTORXYZ/MTOR-Testing: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/spaces/MTORXYZ/MTOR-Testing/resolve/main/tools/knowledge_base_tool.py
- Command line
-
hf download hf://spaces/MTORXYZ/MTOR-Testing/tools/knowledge_base_tool.py
-
curl -L -o knowledge_base_tool.py https://huggingface.co/spaces/MTORXYZ/MTOR-Testing/resolve/main/tools/knowledge_base_tool.py
2.11 kB
| import os | |
| import requests | |
| from openai import AzureOpenAI | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Config | |
| AZURE_SEARCH_ENDPOINT = os.getenv("AZURE_SEARCH_ENDPOINT") | |
| AZURE_SEARCH_KEY = os.getenv("AZURE_SEARCH_KEY") | |
| AZURE_SEARCH_INDEX_NAME = "it-ticket-solutions-index" | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| API_BASE_URL = os.getenv("API_BASE_URL") | |
| MODEL_NAME = os.getenv("MODEL_NAME") | |
| AZURE_OPENAI_API_VERSION = "2024-02-15-preview" | |
| # Clients | |
| openai_client = AzureOpenAI( | |
| api_key=HF_TOKEN, | |
| api_version=AZURE_OPENAI_API_VERSION, | |
| azure_endpoint=API_BASE_URL | |
| ) | |
| def embed_text(text: str): | |
| response = openai_client.embeddings.create( | |
| input=[text], | |
| model=MODEL_NAME | |
| ) | |
| return response.data[0].embedding | |
| def search_similar_solution(query: str, category: str) -> str: | |
| embedding = embed_text(query) | |
| url = f"{AZURE_SEARCH_ENDPOINT}/indexes/{AZURE_SEARCH_INDEX_NAME}/docs/search?api-version=2023-07-01-Preview" | |
| headers = { | |
| "Content-Type": "application/json", | |
| "api-key": AZURE_SEARCH_KEY | |
| } | |
| payload = { | |
| "search": "", | |
| "vectors": [ | |
| { | |
| "value": embedding, | |
| "fields": "embedding", | |
| "k": 3 | |
| } | |
| ], | |
| "select": "category,problem,solution", | |
| "filter": f"category eq '{category}'" | |
| } | |
| response = requests.post(url, headers=headers, json=payload) | |
| if response.status_code != 200: | |
| return f"Error while searching: {response.text}" | |
| results = response.json().get("value", []) | |
| if not results: | |
| return "No matching solutions found." | |
| response_text = "" | |
| for idx, doc in enumerate(results, 1): | |
| response_text += ( | |
| f"\nResult {idx}:\n" | |
| f"Category: {doc.get('category')}\n" | |
| f"Problem: {doc.get('problem')}\n" | |
| f"Solution: {doc.get('solution')}\n" | |
| ) | |
| return response_text | |
| if __name__ == "__main__": | |
| query = "My Outlook crashes every time I open it" | |
| rag_output = search_similar_solution(query, category="Software Bug") | |
| print(rag_output) |