Skip to content

chat api

Nagendra Dhanakeerthi edited this page Oct 30, 2024 · 1 revision

Chat API

The Chat API provides methods to interact with OpenAI's chat completion endpoints.

Basic Usage

client = ChatGPT::Client.new
response = client.chat([
  { role: "user", content: "Hello!" }
])

Message Formatting

Messages should be formatted as an array of hashes with role and content:

messages = [
  { role: "system", content: "You are a helpful assistant." },
  { role: "user", content: "What is Ruby?" },
  { role: "assistant", content: "Ruby is a programming language..." },
  { role: "user", content: "Tell me more!" }
]

Available Roles

  • system: Set behavior and context
  • user: User messages
  • assistant: AI responses
  • function: Function call responses (future implementation)

Parameters

client.chat(messages, {
  model: 'gpt-3.5-turbo',    # Optional, defaults to configured value
  temperature: 0.7,          # Optional (0.0 to 1.0)
  max_tokens: 100,           # Optional
  top_p: 1.0,               # Optional
  n: 1                      # Optional, number of responses
})

Response Format

{
  "choices" => [{
    "message" => {
      "role" => "assistant",
      "content" => "Response content here"
    },
    "finish_reason" => "stop",
    "index" => 0
  }],
  "usage" => {
    "prompt_tokens" => 10,
    "completion_tokens" => 20,
    "total_tokens" => 30
  }
}

Error Handling

begin
  response = client.chat(messages)
rescue ChatGPT::AuthenticationError => e
  # Handle authentication errors
rescue ChatGPT::RateLimitError => e
  # Handle rate limit errors
rescue ChatGPT::APIError => e
  # Handle other API errors
end

Best Practices

  1. Message Context
# Keep conversation context
messages = []
messages << { role: "user", content: "Hello!" }
response = client.chat(messages)
messages << response["choices"][0]["message"]
  1. System Messages
messages = [
  {
    role: "system",
    content: "You are a helpful assistant. Be concise."
  }
]
  1. Error Recovery
def chat_with_retry(messages, max_attempts = 3)
  attempts = 0
  begin
    attempts += 1
    client.chat(messages)
  rescue ChatGPT::RateLimitError => e
    retry if attempts < max_attempts
    raise
  end
end

Examples

Simple Chat

response = client.chat([
  { role: "user", content: "Hello!" }
])
puts response["choices"][0]["message"]["content"]

Conversation Context

messages = [
  { role: "system", content: "You are a helpful assistant." }
]

# First interaction
messages << { role: "user", content: "What is Ruby?" }
response = client.chat(messages)
messages << response["choices"][0]["message"]

# Follow-up
messages << { role: "user", content: "Tell me more!" }
response = client.chat(messages)

With Parameters

response = client.chat(
  messages,
  {
    temperature: 0.7,
    max_tokens: 150
  }
)
Clone this wiki locally