Spaces:
Build error
Build error
Update model to gpt-3.5
Browse files- actions/actions.py +72 -13
actions/actions.py
CHANGED
|
@@ -31,24 +31,36 @@ secret_value_0 = os.environ.get("openai")
|
|
| 31 |
openai.api_key = secret_value_0
|
| 32 |
# Provide your OpenAI API key
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
"""Generate a response using the OpenAI API."""
|
| 36 |
|
| 37 |
# Run the main function from search_content.py and store the results in a variable
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Create context from the results
|
| 41 |
context = "".join([f"#{str(i)}" for i in results])[:2014] # Trim the context to 2014 characters - Modify as necessory
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Generate a response using the OpenAI API
|
| 45 |
-
response = openai.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
max_tokens=max_tokens,
|
| 49 |
-
temperature=temperature
|
| 50 |
-
n=1,
|
| 51 |
-
stop=None,
|
| 52 |
)
|
| 53 |
|
| 54 |
return response.choices[0].text.strip()
|
|
@@ -64,8 +76,9 @@ class GetOpenAIResponse(Action):
|
|
| 64 |
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
| 65 |
|
| 66 |
# Use OpenAI API to generate a response
|
| 67 |
-
query = tracker.latest_message.get('text')
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
# Output the generated response to user
|
| 71 |
dispatcher.utter_message(text=response)
|
|
@@ -210,4 +223,50 @@ class SayHelloWorld(Action):
|
|
| 210 |
|
| 211 |
# Output the generated response to user
|
| 212 |
generated_text = response.choices[0].text
|
| 213 |
-
dispatcher.utter_message(text=generated_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
openai.api_key = secret_value_0
|
| 32 |
# Provide your OpenAI API key
|
| 33 |
|
| 34 |
+
#model_engine="text-davinci-002"
|
| 35 |
+
def generate_openai_response(conversation_data, model_engine="gpt-3.5-turbo", max_tokens=256, temperature=0.5):
|
| 36 |
"""Generate a response using the OpenAI API."""
|
| 37 |
|
| 38 |
# Run the main function from search_content.py and store the results in a variable
|
| 39 |
+
|
| 40 |
+
#results = main_search(query)
|
| 41 |
+
results = main_search(conversation_data["current_user_query"]+conversation_data["previous_user_query"])
|
| 42 |
|
| 43 |
# Create context from the results
|
| 44 |
context = "".join([f"#{str(i)}" for i in results])[:2014] # Trim the context to 2014 characters - Modify as necessory
|
| 45 |
+
|
| 46 |
+
#prompt_template = f"Relevant context: {context}\n\n Answer the question in detail: {query}"
|
| 47 |
+
#previous_user_query = conversation_data["previous_user_query"]
|
| 48 |
+
#previous_bot_response = conversation_data["previous_bot_response"]
|
| 49 |
+
#current_user_query = conversation_data["current_user_query"]
|
| 50 |
+
|
| 51 |
+
# Create the prompt template
|
| 52 |
+
#prompt_template = f"Using Relevant context:{context}\n\n and Previous User Query: {previous_user_query}\n\n Answer the next question in detail:{current_user_query}"
|
| 53 |
+
messages=[
|
| 54 |
+
{"role": "system", "content": f"You are Omdi, a helpful assistant answers Omdena questions Using Relevant context:{context}"},
|
| 55 |
+
{"role": "user", "content": conversation_data["previous_user_query"]},
|
| 56 |
+
{"role": "user", "content": conversation_data["current_user_query"]}
|
| 57 |
+
]
|
| 58 |
# Generate a response using the OpenAI API
|
| 59 |
+
response = openai.ChatCompletion.create(
|
| 60 |
+
model="gpt-3.5-turbo",
|
| 61 |
+
messages= prompt_template,
|
| 62 |
max_tokens=max_tokens,
|
| 63 |
+
temperature=temperature
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
return response.choices[0].text.strip()
|
|
|
|
| 76 |
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
| 77 |
|
| 78 |
# Use OpenAI API to generate a response
|
| 79 |
+
#query = tracker.latest_message.get('text')
|
| 80 |
+
conversation_data = [FollowupAction("action_extract_history")]
|
| 81 |
+
response = generate_openai_response(conversation_data)
|
| 82 |
|
| 83 |
# Output the generated response to user
|
| 84 |
dispatcher.utter_message(text=response)
|
|
|
|
| 223 |
|
| 224 |
# Output the generated response to user
|
| 225 |
generated_text = response.choices[0].text
|
| 226 |
+
dispatcher.utter_message(text=generated_text)
|
| 227 |
+
|
| 228 |
+
class ExtractConversationhistory(Action):
|
| 229 |
+
def name(self) -> Text:
|
| 230 |
+
return "action_extract_history"
|
| 231 |
+
|
| 232 |
+
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
| 233 |
+
conversation_history = tracker.events
|
| 234 |
+
|
| 235 |
+
user_queries = []
|
| 236 |
+
bot_responses = []
|
| 237 |
+
current_user_query = ""
|
| 238 |
+
previous_user_query = None
|
| 239 |
+
previous_bot_response = None
|
| 240 |
+
|
| 241 |
+
for event in conversation_history:
|
| 242 |
+
if event.get("event") == "user":
|
| 243 |
+
user_queries.append(event.get("text"))
|
| 244 |
+
elif event.get("event") == "bot":
|
| 245 |
+
bot_responses.append(event.get("text"))
|
| 246 |
+
|
| 247 |
+
if user_queries:
|
| 248 |
+
if len(user_queries) >= 2:
|
| 249 |
+
previous_user_query = user_queries[-2]
|
| 250 |
+
else:
|
| 251 |
+
pass
|
| 252 |
+
|
| 253 |
+
try:
|
| 254 |
+
current_user_query = user_queries[-1]
|
| 255 |
+
except:
|
| 256 |
+
pass
|
| 257 |
+
|
| 258 |
+
if bot_responses:
|
| 259 |
+
if len(bot_responses) >= 2:
|
| 260 |
+
previous_bot_response = bot_responses[-2]
|
| 261 |
+
else:
|
| 262 |
+
pass
|
| 263 |
+
else:
|
| 264 |
+
pass
|
| 265 |
+
|
| 266 |
+
conversation_data = {
|
| 267 |
+
"previous_user_query": previous_user_query,
|
| 268 |
+
"previous_bot_response": previous_bot_response,
|
| 269 |
+
"current_user_query": current_user_query
|
| 270 |
+
}
|
| 271 |
+
# Now you can use the conversation_data dictionary as needed.
|
| 272 |
+
return conversation_data
|