go-chatgpt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

85 lines
2.9 KiB

  1. package gogpt
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. )
  7. // Chat message role defined by the OpenAI API.
  8. const (
  9. ChatMessageRoleSystem = "system"
  10. ChatMessageRoleUser = "user"
  11. ChatMessageRoleAssistant = "assistant"
  12. )
  13. var (
  14. ErrChatCompletionInvalidModel = errors.New("currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported")
  15. )
  16. type ChatCompletionMessage struct {
  17. Role string `json:"role"`
  18. Content string `json:"content"`
  19. // This property isn't in the official documentation, but it's in
  20. // the documentation for the official library for python:
  21. // - https://github.com/gogpt/gogpt-python/blob/main/chatml.md
  22. // - https://github.com/gogpt/gogpt-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
  23. Name string `json:"name,omitempty"`
  24. }
  25. // ChatCompletionRequest represents a request structure for chat completion API.
  26. type ChatCompletionRequest struct {
  27. Model string `json:"model"`
  28. Messages []ChatCompletionMessage `json:"messages"`
  29. MaxTokens int `json:"max_tokens,omitempty"`
  30. Temperature float32 `json:"temperature,omitempty"`
  31. TopP float32 `json:"top_p,omitempty"`
  32. N int `json:"n,omitempty"`
  33. Stream bool `json:"stream,omitempty"`
  34. Stop []string `json:"stop,omitempty"`
  35. PresencePenalty float32 `json:"presence_penalty,omitempty"`
  36. FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
  37. LogitBias map[string]int `json:"logit_bias,omitempty"`
  38. User string `json:"user,omitempty"`
  39. }
  40. type ChatCompletionChoice struct {
  41. Index int `json:"index"`
  42. Message ChatCompletionMessage `json:"message"`
  43. FinishReason string `json:"finish_reason"`
  44. }
  45. // ChatCompletionResponse represents a response structure for chat completion API.
  46. type ChatCompletionResponse struct {
  47. ID string `json:"id"`
  48. Object string `json:"object"`
  49. Created int64 `json:"created"`
  50. Model string `json:"model"`
  51. Choices []ChatCompletionChoice `json:"choices"`
  52. Usage Usage `json:"usage"`
  53. }
  54. // CreateChatCompletion — API call to Create a completion for the chat message.
  55. func (c *Client) CreateChatCompletion(
  56. ctx context.Context,
  57. request ChatCompletionRequest,
  58. ) (response ChatCompletionResponse, err error) {
  59. model := request.Model
  60. switch model {
  61. case GPT3Dot5Turbo0301, GPT3Dot5Turbo, GPT4, GPT40314, GPT432K0314, GPT432K:
  62. default:
  63. err = ErrChatCompletionInvalidModel
  64. return
  65. }
  66. urlSuffix := "/chat/completions"
  67. req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
  68. if err != nil {
  69. return
  70. }
  71. err = c.sendRequest(req, &response)
  72. return
  73. }