Abstract OpenAI integration to support multiple AI providers #7

Open
opened 2025-06-14 15:40:35 +02:00 by AllfatherHatt · 0 comments

User Story

As a developer, I want the Cog’s AI-calling logic to depend on an abstract interface rather than directly on OpenAI, so that we can swap in other AI providers (Anthropic, Azure, local LLM, etc.) without touching the core code.

Description

  • Define an IAiService interface (or abstract base class) with methods for chat completions, embeddings, and function calls.
  • Refactor the existing OpenAI logic into a concrete OpenAiService that implements IAiService.
  • Update reginaldCog to accept an IAiService instance via its constructor (or a setter), and replace all direct openai.* calls with self.ai_service.*.
  • Add configuration (global or guild) to select which IAiService implementation to use at runtime (defaulting to OpenAI).
  • [OPTIONAL] Provide a mock or stub IAiService implementation for testing.

Acceptance Criteria

  1. An IAiService interface exists with methods for:

     chat_completion(request: ChatRequest) → ChatResponse
    
     create_embedding(input: str) → Embedding
    
     call_function(name: str, args: dict) → dict
    
  2. All direct openai.* calls are moved into OpenAiService : IAiService.

  3. reginaldCog only interacts with IAiService and no longer imports openai.

  4. Configuration allows switching providers without code changes.

  5. A mock IAiService is available and used in unit tests to simulate responses.

  6. Bot behavior remains unchanged when using OpenAiService.

Definition of Done

  • Code reviewed, demo'ed & accepted before merge
  • Code merged
  • Docs updated
# User Story As a developer, I want the Cog’s AI-calling logic to depend on an abstract interface rather than directly on OpenAI, so that we can swap in other AI providers (Anthropic, Azure, local LLM, etc.) without touching the core code. ## Description - Define an IAiService interface (or abstract base class) with methods for chat completions, embeddings, and function calls. - Refactor the existing OpenAI logic into a concrete OpenAiService that implements IAiService. - Update reginaldCog to accept an IAiService instance via its constructor (or a setter), and replace all direct openai.* calls with self.ai_service.*. - Add configuration (global or guild) to select which IAiService implementation to use at runtime (defaulting to OpenAI). - [OPTIONAL] Provide a mock or stub IAiService implementation for testing. ## Acceptance Criteria 1. An IAiService interface exists with methods for: chat_completion(request: ChatRequest) → ChatResponse create_embedding(input: str) → Embedding call_function(name: str, args: dict) → dict 1. All direct openai.* calls are moved into OpenAiService : IAiService. 2. reginaldCog only interacts with IAiService and no longer imports openai. 3. Configuration allows switching providers without code changes. 4. A mock IAiService is available and used in unit tests to simulate responses. 5. Bot behavior remains unchanged when using OpenAiService. ## Definition of Done - [ ] Code reviewed, demo'ed & accepted before merge - [ ] Code merged - [ ] Docs updated
AllfatherHatt added this to the Reginald Camp project 2025-06-14 15:43:07 +02:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Kanium-PUBLIC/KaniumCogs#7
No description provided.