I developed a simulated Jeopardy! game using .NET Core Web App and ChatGPT, which look like this:


The game presents a question along with four possible answers. The player selects one answer, clicks ‘confirm’, and the webpage then indicates whether the selected answer is correct or not. Below is the code that powers this functionality.”
Following are the code behind.
This is the JeopardyQuestionAndAnswerclass that will store the question, answer options, user selected answer and correct answer info.
public class JeopardyQuestionsAndAnswer
{
public string? QuestionText { get; set; } = "";
public IDictionary<char, string> QuestionsOptions { get; set; } = new Dictionary<char, string>();
public char? Answer { get; set; }
public string? AnswerText { get; set; }
public char UserSelectedAnswer { get; set; }
public string? UserSelectedAnswerText { get; set; }
public bool UserAnswerIsCorrect { get; set; }
}
This is the end point of the openAI we will call.
private const string OPENAI_ENDPOINT = "https://api.openai.com/v1/chat/completions"; //endpoint
This is how I called the chatGPT API and get the response.
As you can see, I utilized ChatGPT to generate trivia questions, enforcing a specific format for both questions and answers. This approach simplifies the parsing of the JSON response, allowing for seamless integration into my object structure.
var client = _clientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", OPENAI_API_KEY);
var requestData = new
{
model = "gpt-3.5-turbo",
messages = new[]
{
new
{
role = "system",
content = "Create a trivia question with four multiple-choice answers labeled [A], [B], [C], and [D]. " +
"Follow the following format to provide question and answer: " +
"What is the capital of USA? \n\n [A] Washington DC, [B] New York, [C] Los Angeles, [D] Chicago. Correct Answer: [A],Washington DC."
}
}
};
var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json");
var response = await client.PostAsync(OPENAI_ENDPOINT, content);
This is the JSON response from openAI. We only want the text from ‘content’ node.
{
"id": "chatcmpl-8PsthuczTiFU9JrWN1DQeYaQtTsyy",
"object": "chat.completion",
"created": 1701179917,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Who was the first person to set foot on the moon?\n\n[A] Neil Armstrong\n[B] Buzz Aldrin\n[C] John Glenn\n[D] Alan Shepard\nCorrect Answer: [A] Neil Armstrong"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 80,
"completion_tokens": 41,
"total_tokens": 121
}
}

Then we can have a movie-related question.
Join the conversation! Your thoughts help the community grow.