Skip to content
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

feat: use llamacloud for chat #149

Merged
merged 12 commits into from
Jun 28, 2024
Merged

Conversation

thucpn
Copy link
Collaborator

@thucpn thucpn commented Jun 28, 2024

Summary by CodeRabbit

  • New Features

    • Introduced LlamaCloud integration for enhanced chat functionality.
    • Added support for selecting LlamaCloud as a data source.
    • Implemented environment variable management for LlamaCloud settings.
  • Improvements

    • Enhanced data source selection logic to better handle LlamaCloud.
    • Updated descriptions in chat engine components for more generic usage.
    • Improved dependency management by updating llamaindex package versions.
  • Bug Fixes

    • Fixed how similarityTopK is passed to retrievers in chat engine configurations.

Copy link

changeset-bot bot commented Jun 28, 2024

🦋 Changeset detected

Latest commit: 8fdf640

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 28, 2024

Walkthrough

The changes introduce the LlamaCloud integration for chat functionality, adding support for LlamaCloud as a new data source and vector database. This includes environmental variable setups, dependency updates, and adjustments to the createChatEngine function, alongside TypeScript agents and Python scripts to manage document indexing and retrieval from LlamaCloud.

Changes

File(s) Change Summary
.changeset/tough-pugs-destroy.md Introduced patch for "create-llama" using LlamaCloud for chat
helpers/types.ts Added "llamacloud" to TemplateVectorDB and TemplateDataSourceType types
helpers/env-variables.ts Added new case for "llamacloud" with related environment variables
helpers/typescript.ts Added yellow import from "picocolors" and modified console log message for "llamacloud"
questions.ts Updated getDataSourceChoices and askQuestions to include logic for "llamacloud"
templates/.../engines/typescript/agent/chat.ts Updated query engine description to be more generic
templates/.../vectordbs/typescript/llamacloud/generate.ts Introduced loadAndIndex function for loading documents and creating embeddings in LlamaCloud
templates/.../vectordbs/typescript/llamacloud/index.ts Introduced getDataSource() function for initializing LlamaCloudIndex
templates/.../vectordbs/typescript/llamacloud/shared.ts Added checkRequiredEnvVars function for environment variable checks
.../typescript/nextjs/package.json, .../express/package.json Updated llamaindex package version from 0.4.3 to 0.4.6
templates/types/.../fastapi/pyproject.toml Updated llama-index and llama-index-core versions from "0.10.41" to "0.10.50"
helpers/python.ts Added "llamacloud" case to getAdditionalDependencies function
templates/.../vectordbs/python/llamacloud/generate.py Introduced generate_datasource() function for LlamaCloud indexing
templates/.../vectordbs/python/llamacloud/index.py Introduced get_index() function for initializing LlamaCloudIndex
templates/.../engines/typescript/chat/chat.ts Updated createChatEngine to pass similarityTopK as a property and commented out systemPrompt property assignment

In clouds where llamas prance and play,
New chat lands bright as day,
With types and envs, a journey grand,
Ascript and Python hand in hand.
🎉✨📜


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.

@thucpn thucpn marked this pull request as ready for review June 28, 2024 09:08
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)
questions.ts (1)

Line range hint 574-608: Fix fallthrough in switch case for data source selection.

The case for 'db' is falling through to 'llamacloud', which could lead to unexpected behavior.

+            break;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 64732f0 and 226f4e4.

Files selected for processing (9)
  • .changeset/tough-pugs-destroy.md (1 hunks)
  • helpers/types.ts (2 hunks)
  • questions.ts (5 hunks)
  • templates/components/engines/typescript/agent/chat.ts (2 hunks)
  • templates/components/vectordbs/typescript/llamacloud/generate.ts (1 hunks)
  • templates/components/vectordbs/typescript/llamacloud/index.ts (1 hunks)
  • templates/components/vectordbs/typescript/llamacloud/shared.ts (1 hunks)
  • templates/types/streaming/express/package.json (1 hunks)
  • templates/types/streaming/nextjs/package.json (1 hunks)
Files skipped from review due to trivial changes (2)
  • templates/types/streaming/express/package.json
  • templates/types/streaming/nextjs/package.json
Additional context used
Biome
questions.ts

[error] 585-592: This else clause can be omitted because previous branches break early.

Unsafe fix: Omit the else clause.

(lint/style/noUselessElse)


[error] 574-608: This case is falling through to the next case.

Add a break or return statement to the end of this case to prevent fallthrough.

(lint/suspicious/noFallthroughSwitchClause)

Additional comments not posted (9)
.changeset/tough-pugs-destroy.md (1)

1-5: Clear and concise changeset description.

The changeset correctly labels the patch and succinctly describes its purpose. This will help in tracking the specific changes made in this PR.

templates/components/vectordbs/typescript/llamacloud/shared.ts (1)

7-22: Robust environment variable validation implemented.

This function correctly checks for the presence of required environment variables and throws an error if any are missing. Consider adding more detailed logging or error handling for better operational visibility.

+    console.error(`Missing environment variables: ${missingEnvVars.join(", ")}`);
templates/components/engines/typescript/agent/chat.ts (2)

19-19: Updated description for the query engine.

The updated description is more generic and suitable given the context where multiple data sources could be used.


19-19: Consider robust error handling for JSON parsing.

It's good practice to handle potential parsing errors when reading JSON configuration files.

+  try {
+    toolConfig = JSON.parse(await fs.readFile(configFile, "utf8"));
+  } catch (e) {
+    console.error(`Error parsing ${configFile}: `, e);
+    return;
+  }
helpers/types.ts (2)

30-30: Addition of 'llamacloud' to TemplateVectorDB is approved.

This change is consistent with the PR objectives to integrate LlamaCloud. Ensure that all related components are updated to handle this new type.


40-40: Addition of 'llamacloud' to TemplateDataSourceType is approved.

This change aligns with the PR's aim to support LlamaCloud as a data source. Make sure that the logic handling data sources in the application supports this new type appropriately.

questions.ts (3)

126-129: Handling selection of 'llamacloud' as a data source is approved.

When 'llamacloud' is selected, other data source options are correctly excluded, which aligns with the intended exclusive use of LlamaCloud.


183-185: Addition of 'llamacloud' to data source choices is approved.

This inclusion is consistent with the PR's objectives to support LlamaCloud integration. Ensure that this option is displayed correctly in the UI.


Line range hint 585-592: Remove unnecessary else clause after early return.

This else clause is redundant and can be omitted to improve code clarity.
[REFACTOR_SUGGESTion]

-  } else if (program.dataSources.length > 0 && !program.vectorDb) {
+  if (program.dataSources.length > 0 && !program.vectorDb) {

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)
questions.ts (1)

Line range hint 126-186: Handling of 'llamacloud' in Data Source Choices

The conditional logic to exclude other data source options when "llamacloud" is selected is a thoughtful design choice, ensuring clarity and preventing configuration conflicts. However, static analysis has flagged a potential issue with fall-through in switch cases. This needs to be addressed to avoid unintended behavior.

609,615c609,616
<           case "llamacloud": {
<             program.dataSources.push({
<               type: "llamacloud",
<               config: {},
<             });
<             break;
<           }
---
>           case "llamacloud": {
>             program.dataSources.push({
>               type: "llamacloud",
>               config: {},
>             });
>             break;
>           }
>           default:
>             break;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 226f4e4 and 0923a38.

Files selected for processing (5)
  • helpers/python.ts (1 hunks)
  • questions.ts (5 hunks)
  • templates/components/vectordbs/python/llamacloud/generate.py (1 hunks)
  • templates/components/vectordbs/python/llamacloud/index.py (1 hunks)
  • templates/types/streaming/fastapi/pyproject.toml (1 hunks)
Files skipped from review due to trivial changes (1)
  • templates/types/streaming/fastapi/pyproject.toml
Additional context used
Ruff
templates/components/vectordbs/python/llamacloud/index.py

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)

templates/components/vectordbs/python/llamacloud/generate.py

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)


8-8: Module level import not at top of file

(E402)


9-9: Module level import not at top of file

(E402)

Biome
questions.ts

[error] 585-592: This else clause can be omitted because previous branches break early.

Unsafe fix: Omit the else clause.

(lint/style/noUselessElse)


[error] 574-608: This case is falling through to the next case.

Add a break or return statement to the end of this case to prevent fallthrough.

(lint/suspicious/noFallthroughSwitchClause)

Additional comments not posted (7)
templates/components/vectordbs/python/llamacloud/index.py (3)

1-7: Review of Import Statements and Order

The imports are intentionally ordered to load the .env file before other imports which might depend on these environment variables. This is a common pattern in Python applications that use environment variables extensively. The static analysis warnings about import order can be ignored in this context.

Tools
Ruff

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)


10-10: Logger Setup Approved

Setting up a logger using uvicorn is appropriate for web applications and helps in monitoring and debugging. Good practice!


13-32: Review of get_index Function

The function correctly handles the extraction of environment variables and instantiation of LlamaCloudIndex. Error handling for missing environment variables is well implemented, providing clear messages to the user. This ensures that the function fails gracefully if required configurations are not provided.

templates/components/vectordbs/python/llamacloud/generate.py (3)

12-18: Logger Configuration and Function Initialization

The logger is properly configured at the beginning of the script, and the generate_datasource function correctly initializes settings and logs the process start. This is a good practice for traceability and debugging.


16-39: Core Functionality of generate_datasource

The function effectively uses environment variables to configure the LlamaCloudIndex and handles missing variables robustly. The integration with document loading and indexing is well-implemented, ensuring that the datasource generation is dynamic and configurable.


44-45: Python Main Guard

The use of the main guard (if __name__ == "__main__":) is standard practice in Python scripts. It allows the script to be executed directly or imported without running the main function unintentionally.

helpers/python.ts (1)

121-126: Addition of 'llamacloud' Case in Dependency Management

The addition of the "llamacloud" case in the getAdditionalDependencies function is well-implemented. This maintains consistency and ensures that all dependencies related to "llamacloud" are centralized, making management and updates easier.

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: 3

Outside diff range and nitpick comments (1)
questions.ts (1)

Line range hint 574-608: Add a break statement to prevent fallthrough in switch case.

The 'db' case in the switch statement is missing a break statement, causing unintentional fallthrough to the 'llamacloud' case.

+ break;
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0923a38 and 831df3e.

Files selected for processing (8)
  • helpers/env-variables.ts (1 hunks)
  • helpers/typescript.ts (2 hunks)
  • questions.ts (5 hunks)
  • templates/components/vectordbs/python/llamacloud/generate.py (1 hunks)
  • templates/components/vectordbs/python/llamacloud/index.py (1 hunks)
  • templates/components/vectordbs/typescript/llamacloud/generate.ts (1 hunks)
  • templates/components/vectordbs/typescript/llamacloud/index.ts (1 hunks)
  • templates/components/vectordbs/typescript/llamacloud/shared.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • templates/components/vectordbs/typescript/llamacloud/generate.ts
  • templates/components/vectordbs/typescript/llamacloud/index.ts
  • templates/components/vectordbs/typescript/llamacloud/shared.ts
Additional context used
Ruff
templates/components/vectordbs/python/llamacloud/index.py

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)

templates/components/vectordbs/python/llamacloud/generate.py

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)


8-8: Module level import not at top of file

(E402)


9-9: Module level import not at top of file

(E402)

Biome
helpers/env-variables.ts

[error] 157-170: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

questions.ts

[error] 585-592: This else clause can be omitted because previous branches break early.

Unsafe fix: Omit the else clause.

(lint/style/noUselessElse)


[error] 574-608: This case is falling through to the next case.

Add a break or return statement to the end of this case to prevent fallthrough.

(lint/suspicious/noFallthroughSwitchClause)

Additional comments not posted (5)
templates/components/vectordbs/python/llamacloud/index.py (1)

13-32: Review of function get_index: Ensure proper handling of missing environment variables.

The function correctly checks for the presence of required environment variables and raises an appropriate error if they are missing. This is good practice for robust error handling.

templates/components/vectordbs/python/llamacloud/generate.py (1)

16-41: Review of function generate_datasource: Proper logging and error handling.

The function logs the process of generating an index and raises an error if necessary environment variables are missing. It also uses get_documents to fetch documents for indexing, which should be reviewed to ensure it handles potential exceptions.
[APROVED]

helpers/typescript.ts (1)

108-114: Ensure appropriate logging for LlamaCloud configuration.

The conditional logging for the "llamacloud" vector database is clear and provides necessary information about environment variable settings. This helps in debugging and ensuring that the system is configured correctly.

questions.ts (2)

Line range hint 126-186: Ensure consistent handling of data source choices.

The changes to getDataSourceChoices correctly handle the exclusion of other options when "llamacloud" is already selected. This prevents configuration conflicts and simplifies the user interface.


609-615: Consistent handling of 'llamacloud' data source selection.

The addition of "llamacloud" as a data source is handled correctly, ensuring that it is added to the list of selected data sources when chosen.

templates/components/vectordbs/python/llamacloud/index.py Outdated Show resolved Hide resolved
helpers/env-variables.ts Show resolved Hide resolved
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 831df3e and 3bca368.

Files selected for processing (1)
  • templates/components/engines/typescript/chat/chat.ts (1 hunks)
Additional comments not posted (1)
templates/components/engines/typescript/chat/chat.ts (1)

11-13: Refactor of retriever initialization approved.

The refactor to initialize retriever with similarityTopK directly in the object is cleaner and more readable. Ensure that process.env.TOP_K is always defined or handled gracefully when not defined.

Verification successful

Refactor of retriever initialization approved.

The refactor to initialize retriever with similarityTopK directly in the object is cleaner and more readable. The environment variable process.env.TOP_K is handled gracefully with a default value of 3 if it is not defined.

  • Line 11-13 in templates/components/engines/typescript/chat/chat.ts
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `process.env.TOP_K` is defined in the environment configurations.

# Test: Search for the environment variable definition. Expect: Definition or default handling.
rg --type typescript $'process.env.TOP_K'

Length of output: 75



Script:

#!/bin/bash
# Description: Verify that `process.env.TOP_K` is defined in the environment configurations.

# Find all TypeScript files and search for the environment variable definition within them.
fd -e ts -e tsx -x rg 'process.env.TOP_K' {}

Length of output: 119

Comment on lines +18 to +19
// disable as a custom system prompt disables the generated context
// systemPrompt: process.env.SYSTEM_PROMPT,
Copy link

Choose a reason for hiding this comment

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

Consider removing or clarifying the commented-out systemPrompt.

Commented-out code can lead to confusion if the reason isn't documented. If systemPrompt is no longer needed due to changes in how the chat engine handles prompts, it should be removed to avoid clutter. If it might be needed later, add a comment explaining the conditions under which it would be re-enabled.

-    // systemPrompt: process.env.SYSTEM_PROMOT,
+    // systemPrompt: process.env.SYSTEM_PROMPT, // Temporarily disabled until [condition/reason]
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// disable as a custom system prompt disables the generated context
// systemPrompt: process.env.SYSTEM_PROMPT,
// disable as a custom system prompt disables the generated context
// systemPrompt: process.env.SYSTEM_PROMPT, // Temporarily disabled until [condition/reason]

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

Outside diff range and nitpick comments (3)
questions.ts (3)

Line range hint 126-186: Review data source selection logic for clarity and redundancy.

The logic to exclude other data source options if "llamacloud" is selected could be streamlined. Also, consider using constants for repeated strings like "llamacloud" to avoid typos and enhance maintainability.

- if (selectedDataSource.find((s) => s.type === "llamacloud")) {
+ const LLAMACLOUD = "llamacloud";
+ if (selectedDataSource.some((s) => s.type === LLAMACLOUD)) {

Line range hint 574-608: Address the fall-through issue in the switch-case structure.

There's a missing break statement, which could lead to unintended execution flow.

+ break;  // Add this to prevent fall-through

Line range hint 585-592: Remove unnecessary 'else' clause.

Following the static analysis suggestion, the 'else' clause can be omitted for cleaner and more efficient code.

- else {
-   if (program.dataSources.some((ds) => ds.type === "file")) {
+ if (program.dataSources.some((ds) => ds.type === "file")) {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3bca368 and 8fdf640.

Files selected for processing (3)
  • questions.ts (4 hunks)
  • templates/components/vectordbs/python/llamacloud/generate.py (1 hunks)
  • templates/components/vectordbs/python/llamacloud/index.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • templates/components/vectordbs/python/llamacloud/index.py
Additional context used
Ruff
templates/components/vectordbs/python/llamacloud/generate.py

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)


8-8: Module level import not at top of file

(E402)


9-9: Module level import not at top of file

(E402)

Biome
questions.ts

[error] 585-592: This else clause can be omitted because previous branches break early.

Unsafe fix: Omit the else clause.

(lint/style/noUselessElse)


[error] 574-608: This case is falling through to the next case.

Add a break or return statement to the end of this case to prevent fallthrough.

(lint/suspicious/noFallthroughSwitchClause)

Additional comments not posted (3)
templates/components/vectordbs/python/llamacloud/generate.py (3)

1-9: Reorder imports to comply with PEP 8 standards.

As previously noted and confirmed by static analysis, the import statements should be at the top of the file before any executable code.

Tools
Ruff

5-5: Module level import not at top of file

(E402)


6-6: Module level import not at top of file

(E402)


7-7: Module level import not at top of file

(E402)


8-8: Module level import not at top of file

(E402)


9-9: Module level import not at top of file

(E402)


16-39: Review the main function logic for clarity and robustness.

The generate_datasource function is well-structured, handling environment variable retrieval, error checking, and document processing. However, consider adding more detailed logging at each step to improve traceability and debugging.

+ logger.info("Retrieving environment variables")
+ logger.info("Validating environment variables")
+ logger.info("Retrieving documents")

44-45: Ensure the main guard is properly used.

The if __name__ == "__main__": guard is correctly used to allow this script to be imported without executing generate_datasource immediately. This is a best practice in Python to make modules reusable.

@marcusschiesser marcusschiesser merged commit aeb6fef into main Jun 28, 2024
7 checks passed
@marcusschiesser marcusschiesser deleted the feat/use-llamacloud-for-chat branch June 28, 2024 15:10
This pull request was closed.
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