| import random |
| import re |
| import requests |
|
|
| class Lynx: |
| def __init__(self, responses, context_window=3, api_key=None): |
| """ |
| Initializes the LLM with a dictionary of responses, context window, and RapidAPI key for external search. |
| |
| Args: |
| responses: A dictionary where keys are regex patterns, and values are lists of possible responses. |
| context_window: The number of previous inputs to remember. |
| api_key: The API key for accessing the web search API on RapidAPI. |
| """ |
| self.responses = {re.compile(k, re.IGNORECASE): v for k, v in responses.items()} |
| self.context = [] |
| self.context_window = context_window |
| self.api_key = api_key |
|
|
| def update_context(self, user_input): |
| """Updates the conversation context and maintains the context window size.""" |
| self.context.append(user_input.lower()) |
| if len(self.context) > self.context_window: |
| self.context.pop(0) |
|
|
| def search_web(self, query): |
| """ |
| Searches the web using RapidAPI for relevant information based on the user's input. |
| |
| Args: |
| query: The user's input string. |
| |
| Returns: |
| A string with the search result or a fallback message. |
| """ |
| url = "https://contextualwebsearch-web-search-v1.p.rapidapi.com/api/Search/WebSearchAPI" |
| headers = { |
| "X-RapidAPI-Key": self.api_key, |
| "X-RapidAPI-Host": "contextualwebsearch-web-search-v1.p.rapidapi.com" |
| } |
| params = { |
| "q": query, |
| "pageNumber": 1, |
| "pageSize": 1, |
| "autoCorrect": "true" |
| } |
|
|
| try: |
| response = requests.get(url, headers=headers, params=params) |
| response.raise_for_status() |
| search_results = response.json() |
|
|
| |
| if search_results.get("value"): |
| result = search_results["value"][0] |
| return result.get('snippet', 'No relevant snippet found.') |
| else: |
| return "Sorry, I couldn't find anything relevant." |
| except requests.exceptions.RequestException as e: |
| return f"Error while searching: {e}" |
|
|
| def generate_response(self, user_input): |
| """ |
| Generates a response based on the user's input and the LLM's responses or searches. |
| |
| Args: |
| user_input: The user's input string. |
| |
| Returns: |
| A string representing the LLM's response. |
| """ |
| self.update_context(user_input) |
| |
| |
| for pattern, response_list in self.responses.items(): |
| if pattern.search(user_input): |
| return random.choice(response_list) |
| |
| |
| search_response = self.search_web(user_input) |
| return search_response |
|
|
| |
| responses = { |
| r"\bhello\b": ["Hello!", "Hi there!", "Greetings!"], |
| r"\bhow are you\b": ["I'm doing well, thank you!", "I'm fine.", "I'm functioning as expected."], |
| r"\bgoodbye\b": ["Goodbye!", "See you later!", "Farewell!"], |
| r"\bpython\b": ["Python is a versatile programming language.", "Python is widely used in data science and web development."], |
| } |
|
|
| |
| api_key = "811ee68f6dmshd2ec34da8f73905p149a9ajsncce5d3f12176" |
|
|
| llm = Lynx(responses, api_key=api_key) |
|
|
| while True: |
| user_input = input("You: ") |
| if user_input.lower() == "exit": |
| break |
| response = llm.generate_response(user_input) |
| print("Lynx:", response) |
|
|