Upload get_keywords.py
Browse files- get_keywords.py +16 -13
get_keywords.py
CHANGED
@@ -1,13 +1,12 @@
|
|
1 |
-
from
|
2 |
-
from langchain_core.messages import (
|
3 |
-
HumanMessage,
|
4 |
-
SystemMessage
|
5 |
-
)
|
6 |
-
|
7 |
from rake_nltk import Rake
|
8 |
import nltk
|
9 |
nltk.download('stopwords')
|
10 |
nltk.download('punkt')
|
|
|
|
|
|
|
|
|
11 |
"""
|
12 |
This function takes in user query and returns keywords
|
13 |
Input:
|
@@ -52,12 +51,16 @@ Input:
|
|
52 |
Output: keywords: str
|
53 |
"""
|
54 |
def get_keywords_openai(user_query: str) -> str:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
SystemMessage(content=command),
|
59 |
-
HumanMessage(content=user_query)
|
60 |
]
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
return res
|
|
|
1 |
+
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
2 |
from rake_nltk import Rake
|
3 |
import nltk
|
4 |
nltk.download('stopwords')
|
5 |
nltk.download('punkt')
|
6 |
+
|
7 |
+
# Initialize OpenAI client
|
8 |
+
client = OpenAI()
|
9 |
+
|
10 |
"""
|
11 |
This function takes in user query and returns keywords
|
12 |
Input:
|
|
|
51 |
Output: keywords: str
|
52 |
"""
|
53 |
def get_keywords_openai(user_query: str) -> str:
|
54 |
+
messages = [
|
55 |
+
{"role": "system", "content": "return the keywords of the following query. response should be words separated by commas."},
|
56 |
+
{"role": "user", "content": user_query}
|
|
|
|
|
57 |
]
|
58 |
+
|
59 |
+
completion = client.chat.completions.create(
|
60 |
+
model="gpt-4o-mini",
|
61 |
+
messages=messages,
|
62 |
+
temperature=0
|
63 |
+
)
|
64 |
+
response = completion.choices[0].message.content
|
65 |
+
res = response.replace(",", "")
|
66 |
return res
|