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

Provider/Generator Refactor #185

Closed
6 tasks done
sixlive opened this issue Feb 14, 2025 · 8 comments
Closed
6 tasks done

Provider/Generator Refactor #185

sixlive opened this issue Feb 14, 2025 · 8 comments
Assignees
Labels
help wanted Extra attention is needed planned Item planned on the roadmap

Comments

@sixlive
Copy link
Contributor

sixlive commented Feb 14, 2025

Provider Response Type Refactor

To Do

  • Update Providers
  • Remove Generators / Update Provider Contract
  • Prism::fake updates
  • Update docs
    • Custom Provider Docs
    • Testing docs with Prism::fake

Overview

This refactor aims to improve Prism's architecture by moving request handling and response generation logic from the Generator classes to the individual providers. This change will make Prism more flexible and allow providers to implement their own request/response flow in a way that best suits their specific needs.

Current Implementation

Currently, our Provider interface uses a generic ProviderResponse type:

interface Provider
{
    public function text(TextRequest $request): ProviderResponse;
    public function structured(StructuredRequest $request): ProviderResponse;
    public function embeddings(EmbeddingsRequest $request): EmbeddingsResponse;
}

The Generator classes are responsible for:

  • Managing the request loop
  • Invoking tools
  • Appending tool results
  • Iterating additional calls
  • Building the final response

This creates a rigid structure where all providers must conform to the same request/response pattern, even when their APIs might work differently.

Proposed Changes

We'll update the Provider interface to use specific response types:

interface Provider
{
    public function text(TextRequest $request): TextResponse;
    public function structured(StructuredRequest $request): StructuredResponse;
    public function embeddings(EmbeddingsRequest $request): EmbeddingsResponse;
}

Benefits

  1. Providers can implement their own request/response flow
  2. More flexible architecture for different API patterns
  3. Clearer separation of concerns

Implementation Strategy

To make this transition manageable, we'll:

  1. Update the Provider interface to temporarily accept union types:
public function text(TextRequest $request): TextResponse|ProviderResponse;
  1. Create new response types for each operation type:
  • TextResponse
  • StructuredResponse
  1. Migrate providers one at a time to return the new response types

  2. Update Generator classes to handle both response types during transition

  3. Once all providers are migrated, remove union types and old response types

Help Needed

This is a major refactor that will touch many parts of the codebase. We need help with:

  1. Updating individual providers
  2. Writing tests for new response types
  3. Updating documentation
  4. Code review and testing

Getting Started

Pick a provider to migrate:

  1. Create a fork and new branch refactor/provider-responses
  2. Update the provider to return the new response type
  3. Add tests
  4. Submit a PR

Testing Requirements

  • All existing tests should pass
  • New tests should cover the new response types
  • Integration tests should verify tool handling still works

Questions?

Feel free to ask questions in the comments. This is a significant change to Prism's architecture, so we want to make sure everyone understands the goals and implementation strategy.

@sixlive sixlive added help wanted Extra attention is needed planned Item planned on the roadmap labels Feb 14, 2025
@sixlive sixlive self-assigned this Feb 14, 2025
@ChrisB-TL
Copy link
Contributor

I'll start with Anthropic and Bedrock and pick whatever's left after 😆

@pushpak1300
Copy link
Contributor

I'll pick Gemini, Deepseek, Mistral & XAI in same order.

@sixlive
Copy link
Contributor Author

sixlive commented Feb 22, 2025

I just merged the Ollama refactor as an example of how I think providers could/should look.

@ChrisB-TL
Copy link
Contributor

I'll pick Gemini, Deepseek, Mistral & XAI in same order.

@pushpak1300 have you started on Gemini? If not I have some time this morning and can take on.

@pushpak1300
Copy link
Contributor

i have not yet. @ChrisB-TL

@ChrisB-TL
Copy link
Contributor

@sixlive Bedrock package also refactored. I am assuming we want to still keep it out of the main repo given the AWS SDK dependency? Or would you rather I PR it in?

@sixlive
Copy link
Contributor Author

sixlive commented Feb 23, 2025

@ChrisB-TL lets keep is separate. Before we release the Bedrock package I want to complete #197. I'll prioritize it tomorrow!

@sixlive
Copy link
Contributor Author

sixlive commented Feb 23, 2025

@sixlive sixlive closed this as completed Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed planned Item planned on the roadmap
Projects
None yet
Development

No branches or pull requests

3 participants