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.

local.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package model
  15. import (
  16. "context"
  17. "fmt"
  18. "io"
  19. "net/http"
  20. "strings"
  21. "github.com/sashabaranov/go-openai"
  22. )
  23. type LocalModelProvider struct {
  24. typ string
  25. subType string
  26. deploymentName string
  27. secretKey string
  28. temperature float32
  29. topP float32
  30. frequencyPenalty float32
  31. presencePenalty float32
  32. providerUrl string
  33. apiVersion string
  34. }
  35. func NewLocalModelProvider(typ string, subType string, secretKey string, temperature float32, topP float32, frequencyPenalty float32, presencePenalty float32, providerUrl string) (*LocalModelProvider, error) {
  36. p := &LocalModelProvider{
  37. typ: typ,
  38. subType: subType,
  39. secretKey: secretKey,
  40. temperature: temperature,
  41. topP: topP,
  42. frequencyPenalty: frequencyPenalty,
  43. presencePenalty: presencePenalty,
  44. providerUrl: providerUrl,
  45. }
  46. return p, nil
  47. }
  48. func getLocalClientFromUrl(authToken string, url string) *openai.Client {
  49. config := openai.DefaultConfig(authToken)
  50. config.BaseURL = url
  51. c := openai.NewClientWithConfig(config)
  52. return c
  53. }
  54. func (p *LocalModelProvider) QueryText(question string, writer io.Writer, builder *strings.Builder) error {
  55. var client *openai.Client
  56. if p.typ == "Local" {
  57. client = getLocalClientFromUrl(p.secretKey, p.providerUrl)
  58. } else if p.typ == "Azure" {
  59. client = getAzureClientFromToken(p.subType, p.deploymentName, p.secretKey, p.providerUrl, p.apiVersion)
  60. }
  61. ctx := context.Background()
  62. flusher, ok := writer.(http.Flusher)
  63. if !ok {
  64. return fmt.Errorf("writer does not implement http.Flusher")
  65. }
  66. model := p.subType
  67. temperature := p.temperature
  68. topP := p.topP
  69. frequencyPenalty := p.frequencyPenalty
  70. presencePenalty := p.presencePenalty
  71. respStream, err := client.CreateChatCompletionStream(
  72. ctx,
  73. openai.ChatCompletionRequest{
  74. Model: model,
  75. Messages: []openai.ChatCompletionMessage{
  76. {
  77. Role: openai.ChatMessageRoleUser,
  78. Content: question,
  79. },
  80. },
  81. Stream: true,
  82. Temperature: temperature,
  83. TopP: topP,
  84. FrequencyPenalty: frequencyPenalty,
  85. PresencePenalty: presencePenalty,
  86. },
  87. )
  88. if err != nil {
  89. return err
  90. }
  91. defer respStream.Close()
  92. isLeadingReturn := true
  93. for {
  94. completion, streamErr := respStream.Recv()
  95. if streamErr != nil {
  96. if streamErr == io.EOF {
  97. break
  98. }
  99. return streamErr
  100. }
  101. data := completion.Choices[0].Delta.Content
  102. if isLeadingReturn && len(data) != 0 {
  103. if strings.Count(data, "\n") == len(data) {
  104. continue
  105. } else {
  106. isLeadingReturn = false
  107. }
  108. }
  109. // Write the streamed data as Server-Sent Events
  110. if _, err = fmt.Fprintf(writer, "event: message\ndata: %s\n\n", data); err != nil {
  111. return err
  112. }
  113. flusher.Flush()
  114. // Append the response to the strings.Builder
  115. builder.WriteString(data)
  116. }
  117. return nil
  118. }