-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtag_analyzer.py
More file actions
77 lines (62 loc) · 2.52 KB
/
Copy pathtag_analyzer.py
File metadata and controls
77 lines (62 loc) · 2.52 KB
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
"""
Tag analyzer for extracting and matching tags from user queries.
"""
from typing import List, Dict, Any
import openai
class TagAnalyzer:
"""
Class for extracting and analyzing tags from user queries.
"""
def __init__(self, openai_client: openai.AsyncOpenAI):
"""
Initialize the tag analyzer.
Args:
openai_client: The OpenAI client to use for tag analysis
"""
self.client = openai_client
async def extract_tags(self, query: str, available_tags: List[str]) -> List[str]:
"""
Extract relevant tags from a user query based on available tags.
Args:
query: The user query
available_tags: List of all available tags in the database
Returns:
List of relevant tags extracted from the query
"""
if not available_tags:
return []
# Format the tag list for the prompt
tags_str = ", ".join(available_tags)
# Create prompt for the LLM
prompt = f"""Given a user query and a list of available tags, identify the most relevant tags that match the query.
User query: "{query}"
Available tags: {tags_str}
Return a JSON array of the most relevant tags (maximum 5 tags). Only return tags that are in the available tags list.
Just return the JSON array with no other text. If no relevant tags are found, return an empty array.
Output:"""
# Get tags from the LLM
response = await self.client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": query}
]
)
# Parse the response to extract tags
# Clean up the response to ensure it's valid JSON
response = response.choices[0].message.content.strip()
if response.startswith("```json"):
response = response[7:]
if response.endswith("```"):
response = response[:-3]
response = response.strip()
try:
import json
tags = json.loads(response)
# Ensure we only return tags that are actually in the available_tags
validated_tags = [tag for tag in tags if tag in available_tags]
return validated_tags
except Exception as e:
print(f"Error parsing tags from LLM response: {e}")
print(f"LLM response: {response}")
return []