Create __main__.py
Browse files- __main__.py +101 -0
__main__.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import re
|
3 |
+
import requests
|
4 |
+
|
5 |
+
class Lynx:
|
6 |
+
def __init__(self, responses, context_window=3, api_key=None):
|
7 |
+
"""
|
8 |
+
Initializes the LLM with a dictionary of responses, context window, and RapidAPI key for external search.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
responses: A dictionary where keys are regex patterns, and values are lists of possible responses.
|
12 |
+
context_window: The number of previous inputs to remember.
|
13 |
+
api_key: The API key for accessing the web search API on RapidAPI.
|
14 |
+
"""
|
15 |
+
self.responses = {re.compile(k, re.IGNORECASE): v for k, v in responses.items()}
|
16 |
+
self.context = []
|
17 |
+
self.context_window = context_window
|
18 |
+
self.api_key = api_key # API key for accessing RapidAPI's search results
|
19 |
+
|
20 |
+
def update_context(self, user_input):
|
21 |
+
"""Updates the conversation context and maintains the context window size."""
|
22 |
+
self.context.append(user_input.lower())
|
23 |
+
if len(self.context) > self.context_window:
|
24 |
+
self.context.pop(0)
|
25 |
+
|
26 |
+
def search_web(self, query):
|
27 |
+
"""
|
28 |
+
Searches the web using RapidAPI for relevant information based on the user's input.
|
29 |
+
|
30 |
+
Args:
|
31 |
+
query: The user's input string.
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
A string with the search result or a fallback message.
|
35 |
+
"""
|
36 |
+
url = "https://contextualwebsearch-web-search-v1.p.rapidapi.com/api/Search/WebSearchAPI"
|
37 |
+
headers = {
|
38 |
+
"X-RapidAPI-Key": self.api_key, # Your API Key from RapidAPI
|
39 |
+
"X-RapidAPI-Host": "contextualwebsearch-web-search-v1.p.rapidapi.com"
|
40 |
+
}
|
41 |
+
params = {
|
42 |
+
"q": query,
|
43 |
+
"pageNumber": 1,
|
44 |
+
"pageSize": 1, # Limiting to 1 result for simplicity
|
45 |
+
"autoCorrect": "true"
|
46 |
+
}
|
47 |
+
|
48 |
+
try:
|
49 |
+
response = requests.get(url, headers=headers, params=params)
|
50 |
+
response.raise_for_status()
|
51 |
+
search_results = response.json()
|
52 |
+
|
53 |
+
# If there are search results, take the first snippet or return a fallback message
|
54 |
+
if search_results.get("value"):
|
55 |
+
result = search_results["value"][0] # Take the first search result
|
56 |
+
return result.get('snippet', 'No relevant snippet found.')
|
57 |
+
else:
|
58 |
+
return "Sorry, I couldn't find anything relevant."
|
59 |
+
except requests.exceptions.RequestException as e:
|
60 |
+
return f"Error while searching: {e}"
|
61 |
+
|
62 |
+
def generate_response(self, user_input):
|
63 |
+
"""
|
64 |
+
Generates a response based on the user's input and the LLM's responses or searches.
|
65 |
+
|
66 |
+
Args:
|
67 |
+
user_input: The user's input string.
|
68 |
+
|
69 |
+
Returns:
|
70 |
+
A string representing the LLM's response.
|
71 |
+
"""
|
72 |
+
self.update_context(user_input)
|
73 |
+
|
74 |
+
# First check if any predefined responses match
|
75 |
+
for pattern, response_list in self.responses.items():
|
76 |
+
if pattern.search(user_input):
|
77 |
+
return random.choice(response_list)
|
78 |
+
|
79 |
+
# Query RapidAPI if no predefined responses match
|
80 |
+
search_response = self.search_web(user_input)
|
81 |
+
return search_response
|
82 |
+
|
83 |
+
# Example usage:
|
84 |
+
responses = {
|
85 |
+
r"\bhello\b": ["Hello!", "Hi there!", "Greetings!"],
|
86 |
+
r"\bhow are you\b": ["I'm doing well, thank you!", "I'm fine.", "I'm functioning as expected."],
|
87 |
+
r"\bgoodbye\b": ["Goodbye!", "See you later!", "Farewell!"],
|
88 |
+
r"\bpython\b": ["Python is a versatile programming language.", "Python is widely used in data science and web development."],
|
89 |
+
}
|
90 |
+
|
91 |
+
# Your RapidAPI key (replace with your actual API key from RapidAPI)
|
92 |
+
api_key = "811ee68f6dmshd2ec34da8f73905p149a9ajsncce5d3f12176" # Replace with your own API key
|
93 |
+
|
94 |
+
llm = Lynx(responses, api_key=api_key)
|
95 |
+
|
96 |
+
while True:
|
97 |
+
user_input = input("You: ")
|
98 |
+
if user_input.lower() == "exit":
|
99 |
+
break
|
100 |
+
response = llm.generate_response(user_input)
|
101 |
+
print("Lynx:", response)
|