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

Refactor/all in one openai #43

Merged
merged 25 commits into from
Jun 28, 2024
Merged

Refactor/all in one openai #43

merged 25 commits into from
Jun 28, 2024

Conversation

dockhardman
Copy link
Owner

Server Refactoring

  • Removed the APP_TYPE distinction between llm and agent, simplifying the server architecture.
  • Consolidated server functionality into a single server command, replacing separate agent and llm commands.
  • Refactored the server initialization process in languru/server/app.py, removing the lifespan context manager and simplifying the app creation.

OpenAI Client Integration

  • Introduced a new OpenaiClients class to manage different OpenAI-compatible clients (OpenAI, Azure, Anthropic, Google, Groq, Perplexity, Voyage).
  • Implemented organization type detection based on model names and API types.
  • Added dependency injection for OpenAI clients in various API endpoints.

API Endpoints

  • Refactored chat, completion, embedding, and image generation endpoints to use the new OpenAI client system.
  • Simplified request handling by removing separate logic for llm and agent types.
  • Updated audio-related endpoints to use the new client system.

Model Management

  • Consolidated model lists for different providers (OpenAI, Azure, Anthropic, Google, Groq, Perplexity, Voyage) into a single file.
  • Removed custom model discovery and registration logic, relying on the new OpenaiClients class for model information.

Configuration

  • Updated ServerBaseSettings to remove APP_TYPE and related settings.
  • Simplified logging and path initialization in the server configuration.

Testing

  • Refactored test files to use the new unified server approach, removing distinctions between llm and agent environments.
  • Updated test fixtures to use the new OpenaiClients class and simplified API calls.

Cleanup

  • Removed numerous unused files and directories, particularly in the languru/action/ directory.
  • Deleted custom action classes for various model providers, as they're now handled by the OpenaiClients class.

Docker

  • Updated Dockerfile to install all dependencies with -E all flag instead of separate installations for different components.
  • Removed separate Dockerfile for Google-specific setup.

- Consolidate requirements files: combine server and all requirements
- Update various package versions, including:
  - numpy (1.26.4 -> 2.0.0)
  - openai (1.30.5 -> 1.35.3)
  - google-generativeai (0.5.4 -> 0.7.0)
  - sqlalchemy (2.0.30 -> 2.0.31)
  - pytest (8.2.1 -> 8.2.2)
- Remove unused dependencies (e.g., accelerate, flask, flask-restful)
- Adjust package extras and their dependencies
- Clean up redundant or obsolete entries in poetry.lock

This update simplifies the project's dependency management and brings
core libraries up to their latest versions for improved functionality
and security.
- Remove `main.py` and split functionality into `app.py` and `build.py`
- Simplify `ServerBaseSettings` in `config.py`:
  - Remove `APP_TYPE`, `DEFAULT_PORT`, and optional `PORT`
  - Set default `PORT` to 8680
- Delete `LlmSettings` and `AgentSettings` classes
- Update `create_app` and `run_app` functions in `build.py`
- Add health check endpoint
- Implement `app_lifespan` context manager
- Remove model registration and discovery logic
- Rename main CLI group from `app` to `languru_cli`
- Remove separate `agent` and `llm` groups, replace with single `server` group
- Simplify `server_run` command, removing complex port and action handling
- Remove `AppType` enum and related logic
- Update imports and function calls to reflect new structure
- Streamline server settings by using `ServerBaseSettings`
- Introduce OrganizationType enum with OPENAI, AZURE, ANTHROPIC, GOOGLE, GROQ, PERPLEXITY, and VOYAGE
- Implement to_org_type function for converting string to OrganizationType
- Create organization_type_aliases mapping for easy reference
- Add OrganizationNotFound exception
- Remove unused Enum import from server/config.py
- Change APP_NAME from "languru" to "languru-server" in config.py
- Introduce new file deps/openai_clients.py with OpenaiClients class:
  - Implement client initialization for various AI providers
  - Add depends_openai_client method for dynamic client selection
  - Create _org_to_openai_client helper method for organization-based client mapping
- Introduce new CredentialsNotProvided exception
- Replace ValueError with CredentialsNotProvided in API clients:
  - Anthropic
  - Google
  - Groq
  - Perplexity
  - Voyage
- Refactor OpenaiClients initialization:
  - Implement lazy loading for API clients
  - Add error handling for client initialization
  - Improve logging for uninitialized clients
- Update client retrieval logic in depends_openai_client
- Enhance error handling in _org_to_openai_client method
…ructor

- Import OpenaiClients in languru/server/build.py
- Initialize OpenaiClients in create_app function
- Update OpenaiClients constructor to accept *args and **kwargs
- Remove separate handling for LLM and agent types
- Streamline chat completion request processing
- Integrate OpenAI client dependency
- Simplify stream and non-stream response handling
- Remove unused imports and settings
- Add OpenAI client initialization in openai_clients.py
- Remove separate handlers for LLM and agent app types
- Simplify request handling by using a single OpenAI client
- Remove unused imports and redundant code
- Update function signatures to include OpenAI client parameter
- Adjust API endpoints to use the new OpenAI client dependency
- Simplify chat completion handler to use OpenAI client directly
- Refactor text completion handler for better code organization
- Remove unused imports and simplify dependencies
- Unify handling of normal and streaming requests for completions
- Update router to include OpenAI client dependency
- Remove separate handling for LLM and Agent app types
- Integrate OpenAI client as a dependency in API handlers
- Simplify request handling logic in embeddings, images generations, edits, and variations
- Remove unused imports and redundant code
- Update function signatures to include OpenAI client parameter
- Streamline error handling and model selection process
- Remove app type-specific logic from handlers
- Introduce OpenAI client dependency
- Simplify model and moderation request handling
- Remove unused imports and functions
- Raise 501 Not Implemented for unimplemented features
- Update function signatures to include OpenAI client
- Remove model registration endpoint
- Create new file `languru/types/models.py`
- Define `MODELS_OPENAI` tuple with 28 OpenAI model names
- Add `MODELS_AZURE_OPENAI` tuple using a generator expression
  to create Azure versions of OpenAI model names
- Introduce `depends_openai_client_chat_completion_request` function to handle
  organization type and chat completion request dependencies
- Refactor `chat_completions` endpoint to use the new dependency function
- Implement `org_from_model` method in `OpenaiClients` class to determine
  organization type based on model name
- Update `org_to_openai_client` method (formerly `_org_to_openai_client`)
- Add `default_openai_client` method to `OpenaiClients` class
- Simplify test cases for chat completions
- Update Azure OpenAI client initialization with specific API version
- Import model constants (MODELS_AZURE_OPENAI, MODELS_OPENAI)
- Remove unused code and imports
- Reorganize imports and remove unused TYPE_CHECKING
- Add support for multiple AI providers (Anthropic, Google, Groq, Perplexity, Voyage)
- Implement models() method to return supported models for each provider
- Update ModelsHandler and RetrieveModelHandler to use new models() method
- Add new model lists for various AI providers in types/models.py
- Refactor error handling for model retrieval
- Update API endpoints to use organization_type instead of openai_client
- Replace OpenAI's NotFoundError with custom ModelNotFound exception
- Simplify test suite for model API endpoints
- Update OpenAI clients to use custom exception
- Remove unused imports and clean up code
- Introduce `depends_openai_client_completion_request` function for better dependency injection
- Update `TextCompletionHandler` to use the new dependency
- Simplify `text_completions` endpoint by leveraging the new dependency
- Refactor test cases to use a module-scoped test client fixture
- Remove environment-specific tests and consolidate into unified test cases
- Update imports and remove unused code
- Enhance embeddings endpoint with organization type handling
- Add dependency for OpenAI client and embedding request
- Simplify test structure for embeddings and completions
- Remove unnecessary environment and model discovery mocks
- Update imports and type hints for better code clarity
- Introduce `depends_openai_client_model` and `depends_openai_client_images_generations_request` functions
- Update image generation, editing, and variation endpoints to use new dependency functions
- Simplify test cases by removing separate LLM and agent app tests
- Replace individual test fixtures with a single `test_client` fixture
- Remove unused imports and simplify code structure
- Update type hints and error handling in image-related API functions
- Introduce `depends_openai_client_model` and `depends_openai_client_audio_speech_request` functions
- Update audio endpoints to use new dependency functions
- Refactor `AudioSpeechHandler` to use `dummy_generator_func`
- Simplify test structure by removing separate LLM and agent tests
- Add `test_app_audio_translations` function
- Implement `dummy_generator_func` in `common.py`
- Update imports and remove unused imports
- Adjust error handling and type annotations
- Update audio, images, and moderations handlers to use typed Depends
- Introduce new dependency function for moderation requests
- Adjust type hints for OpenAI client and request objects
- Remove redundant imports and simplify test setup
- Add 'text-moderation-latest' and 'text-moderation-stable' to MODELS_OPENAI

This change improves type safety and readability across the codebase.
- Remove separate LLM and agent app tests
- Introduce a module-scoped test_client fixture
- Simplify health check test to use the new fixture
- Remove unnecessary imports and type checking
- Add root endpoint ("/") to the FastAPI app
- Update CLI entry point in pyproject.toml from "app" to "languru_cli"
- Retain existing "/health" endpoint
- Rename 'languru-agent' to 'languru-server'
- Remove individual LLM service configurations (OpenAI, Google, PPLX, Groq, Anthropic)
- Update Dockerfiles to install 'languru[all]' instead of specific extras
- Remove separate Dockerfiles for Google and Anthropic
- Update logging configuration to use dynamic logger name
- Replace OrganizationNotFound exception with CredentialsNotProvided in OpenAI clients
- Minor code cleanup and import optimizations
Copy link

codecov bot commented Jun 28, 2024

Codecov Report

Attention: Patch coverage is 79.64072% with 102 lines in your changes missing coverage. Please review.

Project coverage is 79.90%. Comparing base (5de7ec4) to head (e18ca87).
Report is 2 commits behind head on master.

Files Patch % Lines
languru/server/deps/openai_clients.py 62.22% 51 Missing ⚠️
languru/types/organizations.py 55.55% 16 Missing ⚠️
languru/cli/main.py 0.00% 14 Missing ⚠️
languru/server/build.py 80.55% 7 Missing ⚠️
languru/server/api/v1/audio.py 91.30% 2 Missing ⚠️
languru/server/api/v1/images.py 90.47% 2 Missing ⚠️
languru/openai_plugins/clients/anthropic.py 50.00% 1 Missing ⚠️
languru/openai_plugins/clients/google.py 50.00% 1 Missing ⚠️
languru/openai_plugins/clients/groq.py 50.00% 1 Missing ⚠️
languru/openai_plugins/clients/pplx.py 50.00% 1 Missing ⚠️
... and 6 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #43      +/-   ##
==========================================
- Coverage   80.04%   79.90%   -0.14%     
==========================================
  Files          73       62      -11     
  Lines        3382     2125    -1257     
==========================================
- Hits         2707     1698    -1009     
+ Misses        675      427     -248     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dockhardman dockhardman merged commit 54e92f8 into master Jun 28, 2024
1 of 3 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.

None yet

1 participant