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: Update Ollama Client in Ollama Connector to use OllamaSharp #6194

Open
wants to merge 30 commits into
base: feature-connectors-ollama
Choose a base branch
from

Conversation

feiyun0112
Copy link
Contributor

fix #5993

RogerBarreto and others added 28 commits January 16, 2024 10:44
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
### Motivation and Context
This PR adds Ollama text embedding support

### Description

Referenced existing implementation of OllamaTextGeneration service and
OpenAITextEmbeddingGenerationService for implementation. I have also
added a sample (Example82_OllamaTextEmbeddingGenerationService), Tested
locally against ollama llama2:7b

- Examples may need to be re-numbered as 80, 81, 82 already exist in
master
- I wasn't able to run all tests as I don't have OpenAI access


![image](https://github.com/microsoft/semantic-kernel/assets/33104478/0aba99fc-9a76-4387-87c7-d1b2ffe9cce2)

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
### Motivation and Context

Restructured the Ollama connector core following the suggestions in the
latest ADR

### Description

Followed the HuggingFace connector on the main branch as a reference.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
This PR adds a total of 35 unit tests for the Ollama connector.

The tests are created with the HuggingFace unit tests as a reference
> GitHub Actions (CI) changes may be required to run the new test
project in the pipeline

I also saw some redundancies in the HuggingFaceTextGeneration unit
tests, for example, in the code below, `var contents = await
sut.GetTextContentsAsync("fake-test");` is redundant, and I found this
redundancy in another test
(`GetStreamingTextContentsShouldHaveModelIdDefinedAsync()`) in the same
file
```csharp
[Fact]
public async Task GetTextContentsShouldHaveModelIdDefinedAsync()
{
    //Arrange
    var sut = new HuggingFaceTextGenerationService("fake-model", endpoint: new Uri("https://fake-random-test-host/fake-path"), httpClient: this._httpClient);

    //Act
    var contents = await sut.GetTextContentsAsync("fake-test");
    this._messageHandlerStub.ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
    {
        Content = new StringContent(@"
        [
            {
                ""generated_text"": ""Why the sky is blue? | Dept. of Science & Mathematics Education | University of Notre Dame\nWhen I was in high school I had a pretty simple conception of reality. I believed that if something made sense to me, then it must also be true. I believed that some problems were so fundamental that I couldn’t understand""
            }
        ]",
        Encoding.UTF8,
        "application/json")
    };

    // Act
    var textContent = await sut.GetTextContentAsync("Any prompt");

    // Assert
    Assert.NotNull(textContent.ModelId);
    Assert.Equal("fake-model", textContent.ModelId);
}

Also fixed one project restructuring issue which was missed in the previous PR, KernelBuilder and ServiceCollection extensions are moved to the root of the Ollama connector project
```

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
### Motivation and Context
Fixes the following build errors

![image](https://github.com/microsoft/semantic-kernel/assets/33104478/a5bc8115-3b86-460d-8010-427b39f4013b)

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
@feiyun0112 feiyun0112 requested a review from a team as a code owner May 11, 2024 15:19
@markwallace-microsoft markwallace-microsoft added the .NET Issue or Pull requests regarding .NET code label May 11, 2024
@markwallace-microsoft markwallace-microsoft added the kernel Issues or pull requests impacting the core kernel label May 11, 2024
@RogerBarreto
Copy link
Member

@feiyun0112 Thanks for the contribution, this issue is currently under analysis.

@RogerBarreto RogerBarreto added the needs discussion Issues that require discussion by the internal Semantic Kernel team before proceeding label May 12, 2024
@RogerBarreto RogerBarreto self-assigned this May 12, 2024
feiyun0112 and others added 2 commits May 30, 2024 04:45
…pletionService.cs

Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
…erationService.cs

Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
@RogerBarreto RogerBarreto deleted the branch microsoft:feature-connectors-ollama May 30, 2024 10:44
@RogerBarreto RogerBarreto reopened this May 31, 2024
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 needs discussion Issues that require discussion by the internal Semantic Kernel team before proceeding .NET Issue or Pull requests regarding .NET code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants