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

minimax.go 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. textv1 "github.com/ConnectAI-E/go-minimax/gen/go/minimax/text/v1"
  9. "github.com/ConnectAI-E/go-minimax/minimax"
  10. )
  11. type MiniMaxModelProvider struct {
  12. subType string
  13. groupID string
  14. apiKey string
  15. temperature float32
  16. }
  17. func NewMiniMaxModelProvider(subType string, groupID string, apiKey string, temperature float32) (*MiniMaxModelProvider, error) {
  18. return &MiniMaxModelProvider{
  19. subType: subType,
  20. groupID: groupID,
  21. apiKey: apiKey,
  22. temperature: temperature,
  23. }, nil
  24. }
  25. func (p *MiniMaxModelProvider) QueryText(question string, writer io.Writer, builder *strings.Builder) error {
  26. ctx := context.Background()
  27. client, _ := minimax.New(
  28. minimax.WithApiToken(p.apiKey),
  29. minimax.WithGroupId(p.groupID),
  30. )
  31. req := &textv1.ChatCompletionsRequest{
  32. Messages: []*textv1.Message{
  33. {
  34. SenderType: "USER",
  35. Text: question,
  36. },
  37. },
  38. Model: p.subType,
  39. Temperature: p.temperature,
  40. }
  41. res, _ := client.ChatCompletions(ctx, req)
  42. flusher, ok := writer.(http.Flusher)
  43. if !ok {
  44. return fmt.Errorf("writer does not implement http.Flusher")
  45. }
  46. flushData := func(data string) error {
  47. if _, err := fmt.Fprintf(writer, "event: message\ndata: %s\n\n", data); err != nil {
  48. return err
  49. }
  50. flusher.Flush()
  51. builder.WriteString(data)
  52. return nil
  53. }
  54. err := flushData(res.Choices[0].Text)
  55. if err != nil {
  56. return err
  57. }
  58. return nil
  59. }