diff --git a/docs/language-models/hosted-models/groq.mdx b/docs/language-models/hosted-models/groq.mdx new file mode 100644 index 0000000000..f0c151e46b --- /dev/null +++ b/docs/language-models/hosted-models/groq.mdx @@ -0,0 +1,72 @@ +--- +title: Groq +--- + +To use Open Interpreter with a model from Groq, simply run: + + + +```bash Terminal +interpreter --model groq/llama3-8b-8192 +``` + +```python Python +from interpreter import interpreter + +interpreter.llm.model = "groq/llama3-8b-8192" +interpreter.llm.api_key = '' +interpreter.chat() +``` + + + +If you are having any issues when passing the `--model`, try adding the `--api_base`: + + + +```bash Terminal +interpreter --api_base "https://api.groq.com/openai/v1" --model groq/llama3-8b-8192 --api_key $GROQ_API_KEY +``` + +```python Python +from interpreter import interpreter + +interpreter.llm.model = "groq/llama3-8b-8192" +interpreter.llm.api_key = '' +interpreter.llm.api_base = "https://api.groq.com/openai/v1" +interpreter.llm.context_window = 32000 +interpreter.chat() +``` + + + +# Supported Models + +We support any model on [Groq's models page:](https://console.groq.com/docs/models) + + + +```bash Terminal +interpreter --model groq/mixtral-8x7b-32768 +interpreter --model groq/llama3-8b-8192 +interpreter --model groq/llama3-70b-8192 +interpreter --model groq/gemma-7b-it +``` + +```python Python +interpreter.llm.model = "groq/mixtral-8x7b-32768" +interpreter.llm.model = "groq/llama3-8b-8192" +interpreter.llm.model = "groq/llama3-70b-8192" +interpreter.llm.model = "groq/gemma-7b-it" +``` + + + +# Required Environment Variables + +Run `export GROQ_API_KEY=''` or place it in your rc file and re-source +Set the following environment variables [(click here to learn how)](https://chat.openai.com/share/1062cdd8-62a1-4aa8-8ec9-eca45645971a) to use these models. + +| Environment Variable | Description | Where to Find | +| -------------------- | ---------------------------------------------------- | ------------------------------------------------------------------- | +| `GROQ_API_KEY` | The API key for authenticating to Groq's services. | [Groq Account Page](https://console.groq.com/keys) | diff --git a/interpreter/core/llm/llm.py b/interpreter/core/llm/llm.py index a539e59a8c..35bcdae472 100644 --- a/interpreter/core/llm/llm.py +++ b/interpreter/core/llm/llm.py @@ -1,4 +1,8 @@ import litellm +from groq import Groq + +groq_client = [None] + import tokentrim as tt from ...terminal_interface.utils.display_markdown_message import ( @@ -9,6 +13,7 @@ from .utils.convert_to_openai_messages import convert_to_openai_messages litellm.suppress_debug_info = True +import os import time @@ -26,6 +31,7 @@ def __init__(self, interpreter): # Settings self.model = "gpt-4-turbo" + self.model = "groq/mixtral-8x7b-32768" # can now use models from groq. `export GROQ_API_KEY="your-key-here")` or use --model self.temperature = 0 self.supports_vision = False self.supports_functions = None # Will try to auto-detect @@ -67,7 +73,7 @@ def run(self, messages): self.supports_functions = False except: self.supports_functions = False - + # Trim image messages if they're there if self.supports_vision: image_messages = [msg for msg in messages if msg["type"] == "image"] @@ -210,7 +216,54 @@ def fixed_litellm_completions(**params): # Run completion first_error = None try: - yield from litellm.completion(**params) + def source(**params): + """Get Completions Using LiteLLM""" + yield from litellm.completion(**params) + + if "model" in params and "groq/" in params["model"]: + + def groq_complete(**params): + if groq_client[0] is None: + groq_client[0] = Groq( + # This is the default and can be omitted + api_key=os.environ.get("GROQ_API_KEY"), + timeout=2, + max_retries=3, + ) + res = ( + groq_client[0] + .chat.completions.create( + messages=params["messages"], + model=params["model"].split("groq/")[1], + ) + .choices[0] + .message.content + ) + return res + + def s(**params): + """Get Completions Using Groq""" + params["stream"] = ( + False # To keep things simple for now, and groq is super fast anyway + ) + word_by_word = False + if word_by_word: + for chunk in groq_complete(**params).split(" "): + yield { + "choices": [ + {"delta": {"type": "message", "content": chunk + " "}} + ] + } + else: + for whole in [groq_complete(**params)]: + yield { + "choices": [ + {"delta": {"type": "message", "content": whole}} + ] + } + + source = s + yield from source(**params) except Exception as e: # Store the first error first_error = e diff --git a/pyproject.toml b/pyproject.toml index 79809ccdc7..f6474da6ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ matplotlib = "^3.8.2" toml = "^0.10.2" posthog = "^3.1.0" tiktoken = "^0.6.0" +groq = "^4.3.0" #Optional dependencies