Skip to content

Starting to add Groq as a provider #131

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

Merged
merged 4 commits into from
Jun 19, 2024
Merged

Conversation

Jac-Zac
Copy link
Contributor

@Jac-Zac Jac-Zac commented Jun 17, 2024

I'd like to propose adding support for Groq as a model provider. I'm not deeply familiar with the existing implementation, but I believe this could be useful. If anyone is interested in help me with it, I'd appreciate the assistance (since I really do not have much time lately). I hope this is a good starting point.

Summary by CodeRabbit

  • New Features

    • Introduced Groq as a new model provider, allowing users to configure models and embeddings specifically for Groq.
    • Added support for setting up Groq API keys and model configuration through user prompts.
  • Improvements

    • Expanded model selection options to include Groq, alongside existing providers like OpenAI and Anthropic.

Copy link

changeset-bot bot commented Jun 17, 2024

🦋 Changeset detected

Latest commit: 49cfb8a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-llama Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

coderabbitai bot commented Jun 17, 2024

Walkthrough

The changes introduce a new provider, Groq, to the system by incorporating the necessary configurations, environment variables, and initialization functions. Specifically, the updates include adding the GROQ_API_KEY environment variable, updating model selection and configuration prompts, and modifying initializations and settings in multiple templates for different frameworks (Express and FastAPI). These additions expand the flexibility and options available for users in selecting and configuring models.

Changes

Files Change Summary
helpers/env-variables.ts Added the GROQ_API_KEY environment variable to the getModelEnvs function for the Groq provider.
helpers/providers/groq.ts Introduced askGroqQuestions function to handle Groq API key and model configuration interactions.
helpers/providers/index.ts Included Groq-related functions and updated askModelConfig to support Groq as a model choice.
helpers/types.ts Added "groq" to the ModelProvider type definition.
.../streaming/express/.../settings.ts Added Groq initialization, imports, and model settings configurations in the Express controller.
.../streaming/fastapi/app/settings.py Added Groq initialization and corresponding imports in the FastAPI settings.
.changeset/pink-terms-cheer.md Documented the addition of Groq as a new model provider.

Sequence Diagrams

sequenceDiagram
    participant User
    participant System
    participant GroqAPI

    User->>System: Choose Groq model
    System->>User: Prompt for API Key
    User->>System: Enter API Key
    System->>GroqAPI: Validate API Key
    GroqAPI-->>System: Confirmation
    System-->>User: API Key validated
    User->>System: Select Model
    System->>GroqAPI: Fetch available models
    GroqAPI-->>System: Return model list
    System-->>User: Display model list
Loading

Poem

In the realm of code, where changes grow,
Groq embarks on a brand-new show.
Models and keys, now intertwined,
In Express and FastAPI, they bind.
With Groq aboard, let queries soar,
The code's horizon broadens more! 🚀🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range and nitpick comments (1)
helpers/env-variables.ts (1)

Line range hint 137-150: The declaration of variables within the switch statement in the getVectorDBEnvs function could lead to scope leakage. It's recommended to wrap these declarations in a block.

case "chroma":
+ {
  const envs = [
    {
      name: "CHROMA_COLLECTION",
      description: "The name of the collection in your Chroma database",
    },
    {
      name: "CHROMA_HOST",
      description: "The API endpoint for your Chroma database",
    },
    {
      name: "CHROMA_PORT",
      description: "The port for your Chroma database",
    },
  ];
  if (framework === "fastapi") {
    envs.push({
      name: "CHROMA_PATH",
      description: `The local path to the Chroma database. 
Specify this if you are using a local Chroma database. 
Otherwise, use CHROMA_HOST and CHROMA_PORT config above`,
    });
  }
  return envs;
+ }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9c9decb and 7c5df81.

Files selected for processing (6)
  • helpers/env-variables.ts (1 hunks)
  • helpers/providers/groq.ts (1 hunks)
  • helpers/providers/index.ts (3 hunks)
  • helpers/types.ts (1 hunks)
  • templates/types/streaming/express/src/controllers/engine/settings.ts (3 hunks)
  • templates/types/streaming/fastapi/app/settings.py (7 hunks)
Additional context used
Biome
helpers/env-variables.ts

[error] 137-150: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause. (lint/correctness/noSwitchDeclarations)

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

Additional comments not posted (7)
helpers/providers/index.ts (3)

6-6: Successfully added import for askGroqQuestions. This ensures the new Groq functionality can be utilized within this module.


36-36: Added Groq as a selectable model provider. This is consistent with the PR's objective to integrate Groq support.


53-55: Integration of Groq into the model configuration logic. Ensure that the implementation of askGroqQuestions correctly handles all edge cases, especially since the PR author mentioned a lack of deep familiarity with the existing codebase.

Verification successful

The implementation of askGroqQuestions appears to be comprehensive and handles various scenarios such as missing API keys, CI environments, and user prompts for model selection. The function ensures that default values are used when necessary and checks for environment variables.

  • The function prompts the user for an API key if it's not provided and falls back to using the GROQ_API_KEY environment variable if the user input is empty.
  • It uses default model values in CI environments or when user prompts are disabled.
  • It allows the user to select the model and embedding model through prompts, ensuring that the configuration is set correctly.

Given this thorough handling of different scenarios, it seems that the function is well-implemented to manage edge cases.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `askGroqQuestions` handles all edge cases.

# Test: Search for `askGroqQuestions` function definition and review its implementation.
ast-grep --lang typescript --pattern $'export async function askGroqQuestions($_) {
  $$$
}'

Length of output: 93



Script:

#!/bin/bash
# Locate the `askGroqQuestions` function definition in the codebase.
rg 'export async function askGroqQuestions' --type ts

Length of output: 122



Script:

#!/bin/bash
# Extract the implementation of the `askGroqQuestions` function from `helpers/providers/groq.ts`.
ast-grep --lang typescript --pattern $'export async function askGroqQuestions($_) {
  $$$
}' helpers/providers/groq.ts

Length of output: 119



Script:

#!/bin/bash
# Extract the implementation of the `askGroqQuestions` function from `helpers/providers/groq.ts`.
rg -A 20 'export async function askGroqQuestions' helpers/providers/groq.ts

Length of output: 565



Script:

#!/bin/bash
# Extract the complete implementation of the `askGroqQuestions` function from `helpers/providers/groq.ts`.
rg -A 50 'export async function askGroqQuestions' helpers/providers/groq.ts

Length of output: 1356



Script:

#!/bin/bash
# Extract the complete implementation of the `askGroqQuestions` function from `helpers/providers/groq.ts`.
rg -A 100 'export async function askGroqQuestions' helpers/providers/groq.ts

Length of output: 1768

helpers/types.ts (1)

4-4: The ModelProvider type has been updated to include "groq". This update is necessary for the type safety and correctness of the new functionality being integrated.

templates/types/streaming/express/src/controllers/engine/settings.ts (1)

94-106: The initGroq function sets up Groq models and embedding models. Ensure that ALL_AVAILABLE_GROQ_MODELS includes all necessary models and that the embedding model map is accurate and up-to-date. It might be beneficial to verify the sources of these models and their compatibility with the Groq API.

Verification successful

The ALL_AVAILABLE_GROQ_MODELS constant is indeed imported from llamaindex/llm/groq, confirming its existence. The embedModelMap is used correctly in multiple places, mapping to specific model types.

  • ALL_AVAILABLE_GROQ_MODELS is imported and used in the code.
  • The embedModelMap appears consistently and aligns with the expected usage.

Based on this information, the concern about the completeness and accuracy of the Groq models and embedding models appears to be addressed.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the completeness and accuracy of Groq models and embedding models.

# Test: Check the content of `ALL_AVAILABLE_GROQ_MODELS` and the embedding model map.
ast-grep --lang typescript --pattern $'const ALL_AVAILABLE_GROQ_MODELS = $$$;'

Length of output: 79



Script:

#!/bin/bash
# Description: Verify the completeness and accuracy of Groq models and embedding models.

# Test 1: Check the declaration of `ALL_AVAILABLE_GROQ_MODELS`.
ast-grep --lang typescript --pattern $'const ALL_AVAILABLE_GROQ_MODELS = $$$;'

# Test 2: Check the usage of `embedModelMap` to ensure it aligns with the models available in the Groq API.
rg 'embedModelMap'

Length of output: 834



Script:

#!/bin/bash
# Description: Further verification of Groq models and embedding models.

# Test 1: Search for any similar pattern that might indicate the presence of the model list.
rg 'AVAILABLE_GROQ_MODELS'

# Test 2: Check the context of `embedModelMap` usage to ensure it matches the available models.
rg -A 5 'embedModelMap'

Length of output: 3911

templates/types/streaming/fastapi/app/settings.py (1)

12-13: The integration of Groq provider into the init_settings function is correctly implemented.

helpers/env-variables.ts (1)

218-226: The addition of the GROQ_API_KEY environment variable is well-structured and follows the existing pattern for other providers. However, ensure that the modelConfig.apiKey is properly validated or sanitized before use.

Copy link
Collaborator

@marcusschiesser marcusschiesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jac-Zac looks great, thanks a lot! just a few questions

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7c5df81 and bb4f6cf.

Files selected for processing (1)
  • templates/types/streaming/nextjs/app/api/chat/engine/settings.ts (2 hunks)
Additional comments not posted (2)
templates/types/streaming/nextjs/app/api/chat/engine/settings.ts (2)

31-33: Integration of Groq in the initSettings switch statement looks good.


Line range hint 31-88: Overall structure and logic of the file with the new Groq integration are consistent and follow existing patterns.

Adding to settings

Adding also here since it might be needed

small updates
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
helpers/env-variables.ts (1)

Line range hint 137-150: Wrap the envs declaration in a block to restrict its scope to the 'chroma' case, addressing the lint error.

- const envs = [
+ {
+   const envs = [
    {
      name: "CHROMA_COLLECTION",
      description: "The name of the collection in your Chroma database",
    },
    {
      name: "CHROMA_HOST",
      description: "The API endpoint for your Chroma database",
    },
    {
      name: "CHROMA_PORT",
      description: "The port for your Chroma database",
    },
  ];
- // TS Version doesn't support config local storage path
+   // TS Version doesn't support config local storage path
  if (framework === "fastapi") {
    envs.push({
      name: "CHROMA_PATH",
      description: `The local path to the Chroma database. 
Specify this if you are using a local Chroma database. 
Otherwise, use CHROMA_HOST and CHROMA_PORT config above`,
    });
  }
+ }
  return envs;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bb4f6cf and 55f622b.

Files selected for processing (7)
  • helpers/env-variables.ts (1 hunks)
  • helpers/providers/groq.ts (1 hunks)
  • helpers/providers/index.ts (3 hunks)
  • helpers/types.ts (1 hunks)
  • templates/types/streaming/express/src/controllers/engine/settings.ts (3 hunks)
  • templates/types/streaming/fastapi/app/settings.py (7 hunks)
  • templates/types/streaming/nextjs/app/api/chat/engine/settings.ts (2 hunks)
Files skipped from review as they are similar to previous changes (6)
  • helpers/providers/groq.ts
  • helpers/providers/index.ts
  • helpers/types.ts
  • templates/types/streaming/express/src/controllers/engine/settings.ts
  • templates/types/streaming/fastapi/app/settings.py
  • templates/types/streaming/nextjs/app/api/chat/engine/settings.ts
Additional context used
Biome
helpers/env-variables.ts

[error] 137-150: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause. (lint/correctness/noSwitchDeclarations)

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

Additional comments not posted (1)
helpers/env-variables.ts (1)

218-226: The integration of the GROQ_API_KEY environment variable is well-implemented.

"all-mpnet-base-v2": "Xenova/all-mpnet-base-v2",
};
Settings.llm = new Groq({
model: process.env.MODEL as keyof typeof ALL_AVAILABLE_GROQ_MODELS,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs modelMap lookup like in settings.py - values of the map need to be of type keyof typeof ALL_AVAILABLE_GROQ_MODELS

Suggested change
model: process.env.MODEL as keyof typeof ALL_AVAILABLE_GROQ_MODELS,
model: modelMap[process.env.MODEL!],

(same also for nextjs)

Copy link
Contributor Author

@Jac-Zac Jac-Zac Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean doing something like this ?

diff --git a/templates/types/streaming/nextjs/app/api/chat/engine/settings.ts b/templates/types/streaming/nextjs/app/api/chat/engine/settings.ts
index 48b0cac..3987cc1 100644
--- a/templates/types/streaming/nextjs/app/api/chat/engine/settings.ts
+++ b/templates/types/streaming/nextjs/app/api/chat/engine/settings.ts
@@ -79,9 +79,17 @@ function initGroq() {
     "all-MiniLM-L6-v2": "Xenova/all-MiniLM-L6-v2",
     "all-mpnet-base-v2": "Xenova/all-mpnet-base-v2",
   };
+
+  const modelMap: Record<string, string> = {
+        "llama3-8b": "llama3-8b-8192",
+        "llama3-70b": "llama3-70b-8192",
+        "mixtral-8x7b": "mixtral-8x7b-32768",
+  }
+
   Settings.llm = new Groq({
-    model: process.env.MODEL as keyof typeof ALL_AVAILABLE_GROQ_MODELS,
+    model: modelMap[process.env.MODEL!],
   });
+
   Settings.embedModel = new HuggingFaceEmbedding({
     modelType: embedModelMap[process.env.EMBEDDING_MODEL!],
   });

Tough honestly I'm not really sure how to do those things I just though of doing things similarly to what was done for anthropic but actually I haven't really understood this first line:

import { ALL_AVAILABLE_ANTHROPIC_MODELS } from "llamaindex/llm/anthropic";

I mean I have never used typescript that is way I struggle a bit with that part

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jac-Zac yes exactly, that looks good. The Groq class supports string type as model, so don't worry about ALL_AVAILABLE_GROQ_MODELS.
This code should run, can you test it on TS?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 55f622b and ce3eb73.

Files selected for processing (2)
  • .changeset/pink-terms-cheer.md (1 hunks)
  • helpers/providers/groq.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • .changeset/pink-terms-cheer.md
Files skipped from review as they are similar to previous changes (1)
  • helpers/providers/groq.ts

I suppose I had to added it in both places
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ce3eb73 and 49cfb8a.

Files selected for processing (2)
  • templates/types/streaming/express/src/controllers/engine/settings.ts (3 hunks)
  • templates/types/streaming/nextjs/app/api/chat/engine/settings.ts (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • templates/types/streaming/express/src/controllers/engine/settings.ts
  • templates/types/streaming/nextjs/app/api/chat/engine/settings.ts

Copy link
Collaborator

@marcusschiesser marcusschiesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jac-Zac thanks that looks great!

@marcusschiesser marcusschiesser merged commit 02ed277 into run-llama:main Jun 19, 2024
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants