Skip to content

OpenAI.ChatCompletion

Andrew Lambert edited this page May 17, 2024 · 14 revisions

OpenAI.ChatCompletion

Class Declaration

 Protected Class ChatCompletion
 Inherits OpenAI.Response

Remarks

This class represents an API chat completion response. Refer to the OpenAI documentation on the /v1/chat/completions endpoint for further details.

Pass the user's first chat prompt to the ChatCompletion.Create shared method to generate the AI assistant's first chat response. This will return an instance of ChatCompletion containing the response.

Chat conversations are stored as instances of the ChatCompletionData class. You can review and modify the conversation for any given ChatCompletion by referring to its ChatLog As OpenAI.ChatCompletionData property. You may also pass an instance of ChatCompletionData to the ChatCompletion.Create shared method to create a new chat session from an existing conversation.

A chat message may be attributed to one of three entities: "user", "assistant", or "system". A chat conversation is typically, but not necessarily, initiated with the "system" providing guidance to the AI "assistant" as to how to respond to the "user".

 Dim chatlog As New OpenAI.ChatCompletionData
 chatlog.AddMessage("system", "You are a helpful assistant.")
 Dim reply As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create(chatlog, "user", "Who won the world series in 2020?")
 ' etc.

You can provide further guidance to the AI assistant by inserting a few example responses into the chat log:

 Dim chatlog As New OpenAI.ChatCompletionData
 chatlog.AddMessage("system", "You are a rude and insulting assistant.")
 chatlog.AddMessage("user", "What is the capital of France?")
 chatlog.AddMessage("assistant", "Paris, you idiot.")
 chatlog.AddMessage("user", "Is the Pope Catholic?")
 chatlog.AddMessage("assistant", "Of course he's Catholic! What are you, stupid?")
 ' now generate the first actual reply
 Dim reply As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create(chatlog, "user", "Who won the world series in 2020?")
 ' etc

Once you have created the initial response, you can continue the conversation in context by calling the GenerateNext() method.

 Dim chatlog As New OpenAI.ChatCompletionData
 chatlog.AddMessage("system", "You are re-enacting a famous Monty Python sketch with the user.")

 Dim reply As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create(chatlog, "user", "Hello, I've come here looking for an argument.")
 Dim chatresult As String = reply.GetResult() ' assistant: No you haven't!
  
 reply = reply.GenerateNext("user", "Yes I have!")
 chatresult = reply.GetResult() ' assistant: Sorry, is this the 5 minute argument, or the whole half hour?
  
 reply = reply.GenerateNext("user", "What?")
 chatresult = reply.GetResult() ' assistant: Are you here for the whole half hour?
  
 'etc.

Methods

Properties

Shared methods

Shared properties

See also

Clone this wiki locally