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

Feature suggestion: Add logs with LogLevel using the Standard logging in .NET #6

Closed
doggy8088 opened this issue Mar 26, 2024 · 12 comments · Fixed by #43
Closed

Feature suggestion: Add logs with LogLevel using the Standard logging in .NET #6

doggy8088 opened this issue Mar 26, 2024 · 12 comments · Fixed by #43
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@doggy8088
Copy link
Contributor

There is not logging support in your core library (the Mscc.GenerativeAI package).

I wish there is a way to debug your library from the logs with proper LogLevel when something went wrong.

The Standard .NET Logging should be a good idea.

@jochenkirstaetter
Copy link
Contributor

Thanks, I will look into it.

There should be an option to leverage dependency injection (DI) to provide access to the ILogger interface. However, that's usually bound to an application host or builder.

Good idea, and a solid addition to the package.

@jochenkirstaetter jochenkirstaetter added enhancement New feature or request help wanted Extra attention is needed labels Mar 26, 2024
@doggy8088
Copy link
Contributor Author

@jochenkirstaetter
I have a simple Embedding usage. It always return Response status code does not indicate success: 400 (Bad Request). error. It because there is no logs comes from your library. I have totally no idea how to debug this. With the same ApiKey, all other APIs are functional. Do you have any idea?

async Task Main()
{
	var googleAI = new GoogleAI(apiKey: Util.GetPassword("GEMINI_API_KEY"));

	var model = googleAI.GenerativeModel(model: Model.Embedding).Dump();

	var response = await model.GetModel().Dump();
	
	var embeddings = await model.EmbedContent(GetMarkdown());
	
	embeddings.Dump();
}

image

@jochenkirstaetter
Copy link
Contributor

jochenkirstaetter commented Mar 27, 2024

It looks like it is using the wrong model. Hmm, no then you would get a NotSupportedException as the method checks for the embedding model.

I'm going to check. What's the content of your markdown? Does it contain double quotes (")?
Probably there's some escaping missing.

@doggy8088
Copy link
Contributor Author

doggy8088 commented Mar 27, 2024

Here is my markdown content:

string GetMarkdown()
{
	return """
Read more about [Mscc.GenerativeAI.Web](https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Web) and how to add it to your ASP.NET (Core) web applications. Read more about [Mscc.GenerativeAI.Google](https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Google).

## Install the package 🖥️

Install the package [Mscc.GenerativeAI](https://www.nuget.org/packages/Mscc.GenerativeAI/) from NuGet. You can install the package from the command line using either the command line or the NuGet Package Manager Console. Or you add it directly to your .NET project.

Add the package using the `dotnet` command line tool in your .NET project folder.

```
> dotnet add package Mscc.GenerativeAI
```

Working with Visual Studio use the NuGet Package Manager to install the package Mscc.GenerativeAI.

```
PM> Install-Package Mscc.GenerativeAI
```

Alternatively, add the following line to your `.csproj` file.

```
<ItemGroup>
    <PackageReference Include="Mscc.GenerativeAI" Version="0.9.0" />
  </ItemGroup>
```

You can then add this code to your sources whenever you need to access any Gemini API provided by Google. This package works for Google AI (Google AI Studio) and Google Cloud Vertex AI.

## Features (as per Gemini analysis) ✦

The provided code defines a C# library for interacting with Google's Generative AI models, specifically the Gemini models. It provides functionalities to:

- **List available models**: This allows users to see which models are available for use.
- **Get information about a specific model**: This provides details about a specific model, such as its capabilities and limitations.
- **Generate content**: This allows users to send prompts to a model and receive generated text in response.
- **Generate content stream**: This allows users to receive a stream of generated text from a model, which can be useful for real-time applications.
- **Generate a grounded answer**: This allows users to ask questions and receive answers that are grounded in provided context.
- **Generate embeddings**: This allows users to convert text into numerical representations that can be used for tasks like similarity search.
- **Count tokens**: This allows users to estimate the cost of using a model by counting the number of tokens in a prompt or response.
- **Start a chat session**: This allows users to have a back-and-forth conversation with a model.
- **Create tuned models**: This allows users to provide samples for tuning an existing model. Currently, only the `text-bison-001` and `gemini-1.0-pro-001` models are supported for tuning

The package also defines various helper classes and enums to represent different aspects of the Gemini API, such as model names, request parameters, and response data.

## Authentication use cases 👥

The package supports the following use cases to authenticate.

- Google AI: [Authentication with an API key](https://ai.google.dev/tutorials/setup)
- Google AI: [Authentication with OAuth](https://ai.google.dev/docs/oauth_quickstart)
- Vertex AI: [Authentication with Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev)
- Vertex AI: [Authentication with OAuth](https://github.com/mscraftsman/generative-ai/blob/main) (using [Mscc.GenerativeAI.Google](https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Google))
- Vertex AI: [Authentication with Service Account](https://github.com/mscraftsman/generative-ai/blob/main) (using [Mscc.GenerativeAI.Google](https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Google))

This applies mainly to the instantiation procedure.
""";
}

@doggy8088
Copy link
Contributor Author

doggy8088 commented Mar 27, 2024

The model.GetModel() issue I mentioned here: #13 I think it's not related to my Embedding usage.

I think the main point is the payload format.

Would you consider expose the full response body into the exception object? If I have that, the debugging experience will be much smoother.

@doggy8088
Copy link
Contributor Author

doggy8088 commented Mar 27, 2024

I tried using Postman to send request.

Here is my request payload which is serialized from EmbedContentRequest object.

var request = new EmbedContentRequest("Hello");
request.TaskType = TaskType.RetrievalDocument;
request.Title = "Hello World";
request.Content.Role = "user";

The JSON is:

{
    "model": "models/embedding-001",
    "content": {
        "parts": [
            {
                "text": "Hello"
            }
        ],
        "role": "user",
        "text": "Hello"
    },
    "taskType": "RETRIEVAL_DOCUMENT",
    "title": "Hello World"
}

The response body is:

{
    "error": {
        "code": 400,
        "message": "Invalid JSON payload received. Unknown name \"text\" at 'content': Cannot find field.",
        "status": "INVALID_ARGUMENT",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.BadRequest",
                "fieldViolations": [
                    {
                        "field": "content",
                        "description": "Invalid JSON payload received. Unknown name \"text\" at 'content': Cannot find field."
                    }
                ]
            }
        ]
    }
}

That's weird. I checked the API document: https://ai.google.dev/api/rest/v1beta/Content#Part
The text field should be valid!

Any idea?

@doggy8088
Copy link
Contributor Author

doggy8088 commented Mar 27, 2024

I found the bug here:

image

I'll raise a new issue for this. See here: #14

@jochenkirstaetter
Copy link
Contributor

That second key "text" is too much, and therefore a Bad Request.

@jochenkirstaetter
Copy link
Contributor

Yes, that should be fine now. Or?

@doggy8088
Copy link
Contributor Author

It works perfectly now.

@jochenkirstaetter
Copy link
Contributor

Great feedback, thanks.

@jochenkirstaetter jochenkirstaetter self-assigned this Sep 18, 2024
@jochenkirstaetter
Copy link
Contributor

Work on .NET logging has started.

Currently, there is a new branch logging available that integrates the NuGet package Microsoft.Extension.Logging into the library. I'm using the LoggerMessageAttribute on various extension methods to provide more details about what's happening inside the classes.

Stay tuned, it's still WIP but an active task for now.

Cheers, JoKi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
2 participants