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: allow passing base_url in openai section of config.toml #328

Merged
merged 1 commit into from
Dec 4, 2024

Conversation

maciejmajek
Copy link
Member

@maciejmajek maciejmajek commented Nov 29, 2024

Purpose

Some local model vendors support OpenAI api.

Proposed Changes

This Pr introduces changes that allow user to specify

  • base_url
    in the openai section of config.toml to use the langchain's OpenAI abstractions in ollama/vllm and so on.

Issues

  • Links to relevant issues

Testing

config.toml

[vendor]
simple_model = "openai"
complex_model = "openai"
embeddings_model = "openai"
...
[openai]
simple_model = "gpt-4o-mini"
complex_model = "qwen2.5-coder:3b"
embeddings_model = "text-embedding-ada-002"
base_url = "http://localhost:11434/v1/" # for openai compatible apis
...
04:50:10 (rai-b2azRMqA-py3.12) mmajek@robo-pc-054 rai ±|feat/openai-compatible-apis|python
Python 3.12.3 (main, Nov  6 2024, 18:32:19) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rai.utils.model_initialization import get_llm_model 
>>> llm = get_llm_model("complex_model")
2024-11-29 16:50:22 robo-pc-054 rai.utils.model_initialization[461106] INFO Using LLM model: openai-qwen2.5-coder:3b
>>> llm.invoke("hello")
2024-11-29 16:50:29 robo-pc-054 httpx[461106] INFO HTTP Request: POST http://localhost:11434/v1/chat/completions "HTTP/1.1 200 OK"
AIMessage(content='Hello! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 10, 'prompt_tokens': 30, 'total_tokens': 40, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'qwen2.5-coder:3b', 'system_fingerprint': 'fp_ollama', 'finish_reason': 'stop', 'logprobs': None}, id='run-d5c590c4-56ed-4174-91f0-f0d78844570f-0', usage_metadata={'input_tokens': 30, 'output_tokens': 10, 'total_tokens': 40, 'input_token_details': {}, 'output_token_details': {}})

Summary by CodeRabbit

  • New Features

    • Enhanced OpenAI configuration support with new parameters: base_url.
    • Introduced a dedicated configuration class for OpenAI settings, improving error handling for API key management.
  • Bug Fixes

    • Improved error handling when the api_key is not set, ensuring better user feedback.

@maciejmajek
Copy link
Member Author

@CodeRabbit full review

Copy link
Contributor

coderabbitai bot commented Nov 29, 2024

Walkthrough

The pull request introduces updates to the config.toml file by adding two parameters, base_url and api_key, under the [openai] section. Additionally, it implements a new data class, OpenAIConfig, in the model_initialization.py file, which extends ModelConfig to include the new attributes. The RAIConfig class is modified to utilize OpenAIConfig, enhancing the handling of OpenAI configurations and improving error handling for the API key.

Changes

File Change Summary
config.toml Added base_url and api_key parameters under the [openai] section.
src/rai/rai/utils/model_initialization.py Introduced OpenAIConfig class with base_url and api_key. Updated RAIConfig to use OpenAIConfig. Modified load_config to instantiate OpenAIConfig. Enhanced get_llm_model to handle api_key retrieval and error management.

Possibly related PRs

  • feat: implement configuration gui #304: The changes in this PR enhance the configuration process of RAI, which is directly related to the updates made in the config.toml file in the main PR, specifically regarding the handling of OpenAI configurations.

Suggested reviewers

  • adamdbrw
  • boczekbartek

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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
Contributor

@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 (1)
src/rai/rai/utils/model_initialization.py (1)

54-58: Consider adding default values for better usability

While the structure is good, consider adding default values:

 @dataclass
 class OpenAIConfig(ModelConfig):
-    base_url: str
-    api_key: str
+    base_url: str = "https://api.openai.com/v1/"
+    api_key: str = ""
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 53b457f and 2317a65.

📒 Files selected for processing (2)
  • config.toml (1 hunks)
  • src/rai/rai/utils/model_initialization.py (5 hunks)
🔇 Additional comments (6)
config.toml (2)

17-17: Document API key precedence and security best practices

The comment indicates that this overrides the environment variable, but consider:

  1. Adding a warning comment about avoiding API key storage in config files
  2. Documenting the precedence order (config vs env var) in the README
  3. Recommending the use of environment variables or secure vaults for production deployments

Let's check if there's existing documentation about API key handling:


16-16: Enforce HTTPS for base_url in production environments

While the default OpenAI URL uses HTTPS, users might configure insecure URLs for local deployments. Consider adding a warning or validation when non-HTTPS URLs are used in production environments to prevent potential security risks.

Let's check if there's any URL validation logic in the codebase:

src/rai/rai/utils/model_initialization.py (4)

18-18: LGTM! Good security practices with SecretStr

The addition of SecretStr for handling sensitive data and cast for type safety are appropriate improvements.

Also applies to: 23-23


82-82: LGTM! Proper type annotation

The update to use OpenAIConfig is consistent with the new configuration structure.


120-120: LGTM! Consistent type casting implementation

The type casting is properly implemented across all vendors, improving type safety and maintainability.

Also applies to: 135-135, 144-144


Line range hint 82-93: Verify documentation and migration impact

The configuration structure changes might require updates to documentation and could affect existing users.

✅ Verification successful

Configuration changes are well-documented and supported

The changes to the configuration structure are properly supported across the codebase:

  • The configuration is managed through a comprehensive configurator tool (src/rai/rai/utils/configurator.py) that guides users through setup
  • Documentation in docs/developer_guide.md clearly references the configuration usage and provides examples
  • The configuration structure in model_initialization.py is well-defined with proper dataclasses and type hints
  • The changes are backward compatible as they add new options (Ollama, tracing) while maintaining existing ones (OpenAI, AWS)

No additional documentation updates are needed as the existing documentation and configuration tool already provide sufficient guidance for users.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for potential documentation files that might need updates
fd -e md -e rst | xargs rg -l "config\.toml|configuration|openai"

# Check for potential test files that might need updates
fd -e py | xargs rg -l "config\.toml|ModelConfig|OpenAIConfig"

Length of output: 487


Script:

#!/bin/bash
# Check the content of documentation files for specific configuration examples
echo "=== Checking docs/developer_guide.md ==="
cat docs/developer_guide.md | rg -A 5 -B 5 "config\.toml|configuration"

echo "=== Checking configurator.py for configuration structure ==="
cat src/rai/rai/utils/configurator.py | rg -A 10 -B 10 "class.*Config"

echo "=== Checking for recent changes in configuration files ==="
git diff HEAD~5 -- src/rai/rai/utils/model_initialization.py src/rai/rai/utils/configurator.py

Length of output: 3070


Script:

#!/bin/bash
# Let's check the actual content of the configuration files
echo "=== Checking model_initialization.py ==="
cat src/rai/rai/utils/model_initialization.py

echo "=== Checking configurator.py ==="
cat src/rai/rai/utils/configurator.py

# Let's also check if there are any example config files
echo "=== Checking for example config files ==="
fd "config.*\.toml" -t f

Length of output: 37264

src/rai/rai/utils/model_initialization.py Outdated Show resolved Hide resolved
Copy link
Member

@boczekbartek boczekbartek left a comment

Choose a reason for hiding this comment

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

@maciejmajek
Haven't tested the functionallity yet, but I have some questions. Could you have a look?

config.toml Outdated Show resolved Hide resolved
@maciejmajek maciejmajek force-pushed the feat/openai-compatible-apis branch 2 times, most recently from 050c31f to 09f483b Compare December 3, 2024 12:56
@maciejmajek maciejmajek changed the title feat: allow passing base_url and api_key in openai section of config.toml feat: allow passing base_url in openai section of config.toml Dec 4, 2024
@maciejmajek maciejmajek force-pushed the feat/openai-compatible-apis branch from aa0e206 to 1d064f0 Compare December 4, 2024 11:43
@boczekbartek
Copy link
Member

@maciejmajek
I cross-checked this feature with debugging-assistant and I have some findings:

  • with ollama implementation of openai api, tool calling fails (tool is not called,but the string is printed)
  • with ollama api tool calling works
  • I'll check with vllm and let you know.

@maciejmajek maciejmajek force-pushed the feat/openai-compatible-apis branch from 7039ed4 to 0a37ab7 Compare December 4, 2024 12:17
@maciejmajek maciejmajek force-pushed the feat/openai-compatible-apis branch from 0a37ab7 to 64180c1 Compare December 4, 2024 12:31
@boczekbartek
Copy link
Member

I tested vllm serve with:

vllm serve --port 8090 --gpu-memory-utilization 0.99 --max_model_len 10000 --enable-auto-tool-choice --tool-call-parser hermes Qwen/Qwen2.5-7B-Instruct

and tool calling worked great through openai API!

@maciejmajek maciejmajek merged commit d9267b8 into development Dec 4, 2024
5 checks passed
@maciejmajek maciejmajek deleted the feat/openai-compatible-apis branch December 4, 2024 13:46
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