-
Notifications
You must be signed in to change notification settings - Fork 60
added Groq Support #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4b1a9fa
5f70f8b
37676a9
5c52006
75a5a2a
f5a39dd
e819f06
f0d4752
d8aa22b
523e702
f6faca4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| toolvenv/* | ||
| #.gitignore | ||
| agents/__pycache__/* | ||
| prompts/__pycache__/* | ||
| tools/__pycache__/* | ||
| models/__pycache__/* | ||
| toolbox/__pycache__/* | ||
| tests/__pycache__/* | ||
| */*.egg-info | ||
| */.pytest_cache | ||
| */build |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| OPENAI_API_KEY: 'YOUR_API_KEY' | ||
| OPENAI_API_KEY: 'YOUR_API_KEY' | ||
| GROQ_API_KEY: 'YOUR_API_KEY' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import requests | ||
| import json | ||
| import os | ||
| from utils.get_keys import load_config | ||
|
|
||
| config_path = os.path.join(os.path.dirname(__file__), '..', 'configs', 'config.yaml') | ||
| load_config(config_path) | ||
|
|
||
| class GroqModel: | ||
| def __init__(self, model, system_prompt, temperature): | ||
| self.model_endpoint = 'https://api.groq.com/openai/v1/chat/completions' | ||
| self.temperature = temperature | ||
| self.model = model | ||
| self.system_prompt = system_prompt | ||
| load_config(config_path) | ||
| self.api_key = os.getenv('GROQ_API_KEY') | ||
| self.headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': f'Bearer {self.api_key}' | ||
| } | ||
|
|
||
|
|
||
| def generate_text(self, prompt): | ||
|
|
||
| payload = { | ||
| "model": self.model, | ||
| "response_format": {"type": "json_object"}, | ||
| "messages": [ | ||
| { | ||
| "role": "system", | ||
| "content": self.system_prompt | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": prompt | ||
| } | ||
| ], | ||
| "stream": False, | ||
| "temperature": self.temperature, | ||
| } | ||
|
|
||
| response_dict = requests.post(self.model_endpoint, headers=self.headers, data=json.dumps(payload)) | ||
| response_json = response_dict.json() | ||
| print(response_json) | ||
| response = json.loads(response_json['choices'][0]['message']['content']) | ||
|
|
||
| print(F"\n\nResponse from Groq model: {response}") | ||
|
|
||
| return response |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # @author Sir Lord Dalibor JONIC, MSc BSc (c | ||
| __author__ = "Sir Lord Dalibor JONIC, MSc BSc" | ||
| __copyright__ = "BeMyInspiration 2024" | ||
| __license__ = "MIT" | ||
| __version__ = "1.0.0" | ||
| __maintainer__ = "Sir Lord Dalibor JONIC, MSc BSc" | ||
| __email__ = "dali.manu@gmail.com" | ||
| __status__ = "Production" | ||
| __description__ = "Google searcher with aration" | ||
|
|
||
| from duckduckgo_search import DDGS | ||
| import time | ||
| import json | ||
|
|
||
| def search(query, num_results=5, delay=2, max_retries=1): | ||
| """ | ||
| Perform a web search using DuckDuckGo and return the top results. | ||
| Args: | ||
| query (str): Search query. | ||
| num_results (int): Number of results to return. At least 5. | ||
| delay (int): Delay between retries in seconds. | ||
| max_retries (int): Maximum number of retries for failed requests. | ||
| Returns: | ||
| list: List of top search results (title, link). | ||
| """ | ||
| # Replace single quotes with double quotes | ||
| input_str_clean = json.dumps(query) | ||
| input_str_clean = input_str_clean.replace("'", "\"") | ||
| # Remove any extraneous characters such as trailing quotes | ||
| input_str_clean = input_str_clean.strip().strip("\"") | ||
| #print (query) | ||
|
|
||
| #print (input_str_clean) | ||
| try: | ||
| parsed_data = json.loads(input_str_clean) | ||
| query_content = parsed_data['query'] | ||
| #print (parsed_data) | ||
| query = query_content | ||
| results = [] | ||
| retries = 0 | ||
| except json.JSONDecodeError as e: | ||
| print(f"JSON decoding error: {e}") | ||
|
|
||
| while retries < max_retries: | ||
| try: | ||
| with DDGS() as ddgs: | ||
| search_results = list(ddgs.text(query, max_results=num_results)) | ||
|
|
||
| for result in search_results: | ||
| title = result['title'] | ||
| link = result['href'] | ||
| results.append((title, link)) | ||
|
|
||
| return results | ||
|
|
||
| except Exception as e: | ||
| print(f"Error during search: {e}") | ||
| retries += 1 | ||
| time.sleep(delay * (2 ** retries)) | ||
|
|
||
| return results | ||
|
|
||
| # Example usage: | ||
| #query = "this is all about ai" | ||
| #search_results = search(query, num_results=5) | ||
| #print(search_results) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # @author Sir Lord Dalibor JONIC, MSc BSc (c | ||
| #__author__ = "Sir Lord Dalibor JONIC, MSc BSc" | ||
| #__copyright__ = "BeMyInspiration 2024" | ||
| #__license__ = "MIT" | ||
| #__version__ = "1.0.0" | ||
| #__maintainer__ = "Sir Lord Dalibor JONIC, MSc BSc" | ||
| #__email__ = "dali.manu@gmail.com" | ||
| #__status__ = "Production" | ||
| #__description__ = "Google searcher with aration" | ||
| import requests | ||
| import time | ||
| from bs4 import BeautifulSoup # type: ignore | ||
| from urllib.parse import quote_plus | ||
|
|
||
| def search(query, num_results=5, delay=2, max_retries=3): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesnt work, the search parameter is a json string, not python parameters...
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonasbg
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonasbg check this out :) |
||
| """ | ||
| Perform a web search and return the top results. | ||
| Args: | ||
| query (str): Search query. | ||
| num_results (int): Number of results to return. Do at least 5 please. | ||
| delay (int): Delay between requests in seconds. | ||
| max_retries (int): Maximum number of retries for failed requests. | ||
| Returns: | ||
| list: List of top search results (title, link). | ||
| """ | ||
| query = str(query) # Convert query to string | ||
| search_url = f"https://www.google.com/search?q={quote_plus(query)}" | ||
| headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0'} | ||
| results = [] | ||
| retries = 0 | ||
|
|
||
| while retries < max_retries: | ||
| try: | ||
| with requests.get(search_url, headers=headers, timeout=10) as response: | ||
| response.raise_for_status() | ||
| soup = BeautifulSoup(response.text, "html.parser") | ||
| result_divs = soup.find_all('div', class_='%yuRU%') | ||
| #result_divs = soup.find_all('div') | ||
| for result in result_divs[:num_results]: | ||
| title = result.find('h3').get_text() | ||
| link = result.find('a')['href'] | ||
| results.append((title, link)) | ||
| return results | ||
| except (requests.RequestException, ValueError) as e: | ||
| print(f"Error during search: {e}") | ||
| retries += 1 | ||
| time.sleep(delay * (2 ** retries)) | ||
|
|
||
| return results | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can't be correct?