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

Parsing the OpenAI messages json #99

Closed
slreznit opened this issue Jun 30, 2024 · 5 comments
Closed

Parsing the OpenAI messages json #99

slreznit opened this issue Jun 30, 2024 · 5 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@slreznit
Copy link

slreznit commented Jun 30, 2024

Hi,

I would like to use this SDK to Parse the messages Json and then iterate it and access the different fields

For example, can I do this?

 var messages = """
     [
         {
               "role": "system",
               "content": "You are an AI assistant..."
         },
         {
               "role": "user",
               "content": "some user question",
         }
     ]
     """;
List<ChatMessage> p = ParseChatRequest(messages);
foreach (var message in p)
{
    Print($"{p.role}, {p.content}").
}
@joseharriaga joseharriaga self-assigned this Jul 12, 2024
@joseharriaga joseharriaga added the documentation Improvements or additions to documentation label Jul 13, 2024
@sunnynagavo
Copy link

public class ChatMessage
{
    public string Role { get; set; }
    public string Content { get; set; }
}

static List<ChatMessage> ParseChatRequest(string json)
{
    return JsonSerializer.Deserialize<List<ChatMessage>>(json);
}

you can simply do this right ?

@joseharriaga
Copy link
Collaborator

Thank you for reaching out, @slreznit ! I have an open PR to add an example that illustrates how to serialize/deserialize a list of messages:
🔗 #124

Specifically, this file:
🔗 https://github.com/openai/openai-dotnet/pull/124/files#diff-5813b799855b3942f5ae2dac0014c17b8fa0088683506455676d656ef359cf7a

I would also like to add that we're working on making this simpler in the future. 😊

@omri08
Copy link

omri08 commented Jan 29, 2025

Hello @joseharriaga,
We're unable to parse messages with a content type of input_audio using the example you shared. I guess that's because they are considered Conversation instead of ChatMessage.
Could you please share an example of how to parse this type of content?

(We're also not able to parse ChatMessage when the role is set to Developer)

@joseharriaga
Copy link
Collaborator

Hello, @omri08! We're preparing a fix for this issue, and our current estimate is to release it next week. Please stay tuned. 🙂

@omri08
Copy link

omri08 commented Jan 30, 2025

You are the best 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants