Skip to content

Implement chats in a MAUI app with ChatGPT valid for a session #373

Answered by StephenHodgson
erossini asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @erossini

There is a Conversation class you can use for exactly this purpose.

I use it like so:

    using var api = new OpenAIClient();
    var conversation = new Conversation(
        [
            new(Role.System, "You are a helpful assistant designed to output JSON."),
            new(Role.User, "Who won the world series in 2020?")
        ]);
        
    var chatRequest = new ChatRequest(conversation.Messages, Model.GPT4o, 
                                      responseFormat: ChatResponseFormat.Json);
    var response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
    conversation.AppendMessage(response.FirstChoice.Message);

You should be able to serialize this object …

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@erossini
Comment options

@StephenHodgson
Comment options

@erossini
Comment options

@StephenHodgson
Comment options

Answer selected by StephenHodgson
Comment options

You must be logged in to vote
1 reply
@StephenHodgson
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #372 on October 02, 2024 15:14.