Spaces:
Sleeping
Sleeping
Revert actions to 3 days ago
Browse files- actions/actions.py +6 -63
actions/actions.py
CHANGED
@@ -31,25 +31,15 @@ secret_value_0 = os.environ.get("openai")
|
|
31 |
openai.api_key = secret_value_0
|
32 |
# Provide your OpenAI API key
|
33 |
|
34 |
-
|
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 |
-
context = 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 |
|
54 |
# Generate a response using the OpenAI API
|
55 |
response = openai.Completion.create(
|
@@ -74,9 +64,8 @@ class GetOpenAIResponse(Action):
|
|
74 |
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
75 |
|
76 |
# Use OpenAI API to generate a response
|
77 |
-
|
78 |
-
|
79 |
-
response = generate_openai_response(conversation_data)
|
80 |
|
81 |
# Output the generated response to user
|
82 |
dispatcher.utter_message(text=response)
|
@@ -221,50 +210,4 @@ class SayHelloWorld(Action):
|
|
221 |
|
222 |
# Output the generated response to user
|
223 |
generated_text = response.choices[0].text
|
224 |
-
dispatcher.utter_message(text=generated_text)
|
225 |
-
|
226 |
-
class ExtractConversationhistory(Action):
|
227 |
-
def name(self) -> Text:
|
228 |
-
return "action_extract_history"
|
229 |
-
|
230 |
-
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
231 |
-
conversation_history = tracker.events
|
232 |
-
|
233 |
-
user_queries = []
|
234 |
-
bot_responses = []
|
235 |
-
current_user_query = ""
|
236 |
-
previous_user_query = None
|
237 |
-
previous_bot_response = None
|
238 |
-
|
239 |
-
for event in conversation_history:
|
240 |
-
if event.get("event") == "user":
|
241 |
-
user_queries.append(event.get("text"))
|
242 |
-
elif event.get("event") == "bot":
|
243 |
-
bot_responses.append(event.get("text"))
|
244 |
-
|
245 |
-
if user_queries:
|
246 |
-
if len(user_queries) >= 2:
|
247 |
-
previous_user_query = user_queries[-2]
|
248 |
-
else:
|
249 |
-
pass
|
250 |
-
|
251 |
-
try:
|
252 |
-
current_user_query = user_queries[-1]
|
253 |
-
except:
|
254 |
-
pass
|
255 |
-
|
256 |
-
if bot_responses:
|
257 |
-
if len(bot_responses) >= 2:
|
258 |
-
previous_bot_response = bot_responses[-2]
|
259 |
-
else:
|
260 |
-
pass
|
261 |
-
else:
|
262 |
-
pass
|
263 |
-
|
264 |
-
conversation_data = {
|
265 |
-
"previous_user_query": previous_user_query,
|
266 |
-
"previous_bot_response": previous_bot_response,
|
267 |
-
"current_user_query": current_user_query
|
268 |
-
}
|
269 |
-
# Now you can use the conversation_data dictionary as needed.
|
270 |
-
return conversation_data
|
|
|
31 |
openai.api_key = secret_value_0
|
32 |
# Provide your OpenAI API key
|
33 |
|
34 |
+
def generate_openai_response(query, model_engine="text-davinci-002", max_tokens=124, temperature=0.8):
|
|
|
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 |
+
results = main_search(query)
|
|
|
|
|
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 |
+
prompt_template = f"Relevant context: {context}\n\n Answer the question in detail: {query}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Generate a response using the OpenAI API
|
45 |
response = openai.Completion.create(
|
|
|
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 |
+
response = generate_openai_response(query)
|
|
|
69 |
|
70 |
# Output the generated response to user
|
71 |
dispatcher.utter_message(text=response)
|
|
|
210 |
|
211 |
# Output the generated response to user
|
212 |
generated_text = response.choices[0].text
|
213 |
+
dispatcher.utter_message(text=generated_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|