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

.Net [ChatRoles] 2. Completion service (ChatCompletion / TextCompletion) selection by SemanticFunction #3256

Closed
1 of 2 tasks
SergeyMenshykh opened this issue Oct 23, 2023 · 6 comments
Assignees
Labels
kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code sk team issue A tag to denote issues that where created by the Semantic Kernel team (i.e., not the community)
Milestone

Comments

@SergeyMenshykh
Copy link
Member

SergeyMenshykh commented Oct 23, 2023

As an engineer working on SK,
I want to have a mechanism allowing semantic function to select either text or chat completion API/Connector for execution
So, text prompts are handled by text completion connectors
And chat prompts are handled by chat completion connectors
Or chat prompt handled by text completion connectors and the other way around for fallback scenarios

Scope:

  1. Create ADR and present it to the team.
  2. Collect feedback and implement the completion service selection functionality.

Tasks

Preview Give feedback
@SergeyMenshykh SergeyMenshykh self-assigned this Oct 23, 2023
@SergeyMenshykh SergeyMenshykh converted this from a draft issue Oct 23, 2023
@shawncal shawncal added .NET Issue or Pull requests regarding .NET code triage labels Oct 23, 2023
@SergeyMenshykh SergeyMenshykh added enhancement kernel Issues or pull requests impacting the core kernel and removed triage labels Oct 23, 2023
@SergeyMenshykh SergeyMenshykh added this to the v1.0.0 milestone Oct 23, 2023
@SergeyMenshykh
Copy link
Member Author

SergeyMenshykh commented Oct 23, 2023

@SergeyMenshykh SergeyMenshykh moved this from Sprint: Planned to Sprint: In Progress in Semantic Kernel Oct 24, 2023
@evchaki evchaki added the sk team issue A tag to denote issues that where created by the Semantic Kernel team (i.e., not the community) label Oct 24, 2023
@SergeyMenshykh SergeyMenshykh changed the title .Net Select completion service type based on prompt completion type .Net Completion service (ChatCompletion / TextCompletion) selection by SemanticFunction Oct 24, 2023
@SergeyMenshykh SergeyMenshykh changed the title .Net Completion service (ChatCompletion / TextCompletion) selection by SemanticFunction .Net 2. Completion service (ChatCompletion / TextCompletion) selection by SemanticFunction Oct 27, 2023
@SergeyMenshykh SergeyMenshykh changed the title .Net 2. Completion service (ChatCompletion / TextCompletion) selection by SemanticFunction .Net [ChatRoles] 2. Completion service (ChatCompletion / TextCompletion) selection by SemanticFunction Oct 27, 2023
@matthewbolanos matthewbolanos moved this from Sprint: In Progress to Sprint: In Review in Semantic Kernel Nov 8, 2023
@markwallace-microsoft markwallace-microsoft moved this from Sprint: In Review to Sprint: Done in Semantic Kernel Nov 13, 2023
@stekiri
Copy link

stekiri commented Mar 13, 2024

@dmytrostruk, I read the ADR and it seems that the described logic has not been implemented. A different logic seems to have been introduced in #4085. Is the ADR obsolete?

I stumbled upon it in the following scenario:

  • Custom service that implements both IChatCompletionService and ITextGenerationService.
  • Chat completion implementation is chosen without taking into account the logic of the ADR.

@dmytrostruk
Copy link
Member

@stekiri Thank you for this comment. You are correct, the ADR which we added previously is kind of obsolete, because later we decided to proceed with other approach, which wasn't described in the documentation. We created a new ADR that explains new approach and we updated status of previous ADR in this PR: #5479

Do you have a specific scenario that is not covered by current approach?

@stekiri
Copy link

stekiri commented Mar 15, 2024

Yes @dmytrostruk, my scenario is when a service implements both IChatCompletionService and ITextGenerationService.

My reasoning would be:
If a user implements ITextGenerationService for a service, then the user made a conscious choice that he considers it being important (and maybe better as chat completion?) for a given use case. Therefore, it should take precedence over IChatCompletionService if there is no chat in his prompt.

Some code is worth more than a thousand words, so I made this small draft PR to communicate what I had in mind. My logic might be flawed, so maybe I miss some use cases that would not be supported with such a change.

@dmytrostruk
Copy link
Member

dmytrostruk commented Mar 15, 2024

If a user implements ITextGenerationService for a service, then the user made a conscious choice that he considers it being important (and maybe better as chat completion?) for a given use case. Therefore, it should take precedence over IChatCompletionService if there is no chat in his prompt.

@stekiri I think this scenario is supported, because user could place text generation logic in separate class and implement only ITextGenerationService interface. If user has some common logic for text generation and chat completion that should be re-used in both services, instead of implementing 2 interfaces in the same class, user could create 2 classes, implement both interfaces separately, and put common logic in base class. In order to be able to switch between different services, it's possible to create multiple kernel instances, where one of them will rely on text generation and another one on chat completion.

Some code is worth more than a thousand words, so I made this small draft PR to communicate what I had in mind. My logic might be flawed, so maybe I miss some use cases that would not be supported with such a change.

Thanks for the PR, it definitely helps to understand the idea. The scenario with fallback chat completion may work for now, but I think it will be hard to scale it as soon as we will add support for other types of services (image, audio etc). The solution with placing text generation and chat completion in separate classes should work as expected.

@stekiri
Copy link

stekiri commented Mar 18, 2024

Thanks for taking the time to explain your thoughts in detail @dmytrostruk 🙏

I agree that there are many ways to handle the case, so no worries, it is not blocking me in any way. I am a noobie exploring semantic kernel, I just had the feeling that the implemented logic is surprising.

I finally chose to call kernel.ServiceSelector.TrySelectAIService<ITextGenerationService>(...) + service.GetTextContentsAsync(...) to make it explicit and invoke the text generation service directly. Probably not ideal either, because I guess using kernel.InvokeAsync(...) should be preferred to be future proof.

I had another thought, but I'm not sure if it's worth much. I was wondering if it couldn't be useful to make the decision logic a bit more explicit (and maybe less surprising) by telling the kernel what we expect him to return. For example:

  • If we invoke kernel.InvokeStreamingAsync<StreamingChatMessageContent>, we want to target a IChatCompletionService.
  • If we invoke kernel.InvokeStreamingAsync<StreamingTextContent>, we want to target a ITextGenerationService.
  • This can easily be extended to other services (like IImageGenerationService etc) in the future.

But maybe you already have better strategies in mind for the future, so no worries if my comment is not on point 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code sk team issue A tag to denote issues that where created by the Semantic Kernel team (i.e., not the community)
Projects
Archived in project
Development

No branches or pull requests

5 participants