Created
December 14, 2023 10:00
-
-
Save ehzawad/e4c4309d120c99935e27739f04574b54 to your computer and use it in GitHub Desktop.
Rasa Test Py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
def send_to_rasa(sender_id, message, rasa_server_url): | |
"""Send a message to Rasa and return the bot's response.""" | |
try: | |
response = requests.post( | |
f"{rasa_server_url}/webhooks/rest/webhook", | |
json={"sender": sender_id, "message": message, "respType": "F", "inputType": "AN", "inputKey": "", "ivrStatus": "STOPPED", "cli": "0111111111", "metadata": "bn", "va_id": "AA"} | |
) | |
return response.json() | |
except requests.RequestException as e: | |
print(f"Request failed: {e}") | |
return None | |
def test_conversation(file_path, rasa_server_url, output_file): | |
with open(file_path, 'r', encoding='utf-8') as file: | |
lines = file.read().strip().split('\n') | |
sender_id = "user123" | |
success, failure = 0, 0 | |
results = [] | |
for i, line in enumerate(lines): | |
if '[C]:' in line: | |
user_message = line.split('[C]:')[-1].strip() | |
time.sleep(1) # Delay added between requests | |
bot_responses = send_to_rasa(sender_id, user_message, rasa_server_url) | |
if bot_responses: | |
predicted_response = bot_responses[0].get('text', '') | |
results.append(f"User: {user_message}\nBot: {predicted_response}\n") | |
if '[B]:' in lines[i+1]: | |
expected_response = lines[i+1].split('[B]:')[-1].strip() | |
if predicted_response == expected_response: | |
success += 1 | |
else: | |
failure += 1 | |
print(f"User: {user_message}\nBot: {predicted_response}\n") | |
else: | |
results.append(f"Failed to get response for: {user_message}\n") | |
failure += 1 | |
total_tests = success + failure | |
accuracy = (success / total_tests) * 100 if total_tests > 0 else 0 | |
summary = f"\nTest Completed. Accuracy: {accuracy}% (Success: {success}, Failure: {failure})\n" | |
print(summary) | |
results.append(summary) | |
with open(output_file, 'w', encoding='utf-8') as file: | |
file.writelines(results) | |
if __name__ == "__main__": | |
rasa_server_url = "http://localhost:5004" | |
test_file_path = "path/to/test_conversation.txt" | |
output_file_path = "path/to/test_results.txt" | |
test_conversation(test_file_path, rasa_server_url, output_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment