vtiyyal1 commited on
Commit
da066ce
·
verified ·
1 Parent(s): adef65e

Upload get_keywords.py

Browse files
Files changed (1) hide show
  1. get_keywords.py +16 -13
get_keywords.py CHANGED
@@ -1,13 +1,12 @@
1
- from langchain_openai import ChatOpenAI
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
- llm = ChatOpenAI(temperature=0.0)
56
- command = "return the keywords of the following query. response should be words separated by commas. "
57
- message = [
58
- SystemMessage(content=command),
59
- HumanMessage(content=user_query)
60
  ]
61
- response = llm(message)
62
- res = response.content.replace(",", "")
 
 
 
 
 
 
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