-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package groq | ||
|
||
import ( | ||
"one-api/common/requester" | ||
"one-api/model" | ||
"one-api/providers/base" | ||
"one-api/providers/openai" | ||
) | ||
|
||
// 定义供应商工厂 | ||
type GroqProviderFactory struct{} | ||
|
||
// 创建 GroqProvider | ||
func (f GroqProviderFactory) Create(channel *model.Channel) base.ProviderInterface { | ||
return &GroqProvider{ | ||
OpenAIProvider: openai.OpenAIProvider{ | ||
BaseProvider: base.BaseProvider{ | ||
Config: getConfig(), | ||
Channel: channel, | ||
Requester: requester.NewHTTPRequester(*channel.Proxy, openai.RequestErrorHandle), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func getConfig() base.ProviderConfig { | ||
return base.ProviderConfig{ | ||
BaseURL: "https://api.groq.com/openai", | ||
ChatCompletions: "/v1/chat/completions", | ||
} | ||
} | ||
|
||
type GroqProvider struct { | ||
openai.OpenAIProvider | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package groq | ||
|
||
import ( | ||
"net/http" | ||
"one-api/common" | ||
"one-api/common/requester" | ||
"one-api/providers/openai" | ||
"one-api/types" | ||
) | ||
|
||
func (p *GroqProvider) CreateChatCompletion(request *types.ChatCompletionRequest) (openaiResponse *types.ChatCompletionResponse, errWithCode *types.OpenAIErrorWithStatusCode) { | ||
p.getChatRequestBody(request) | ||
|
||
req, errWithCode := p.GetRequestTextBody(common.RelayModeChatCompletions, request.Model, request) | ||
if errWithCode != nil { | ||
return nil, errWithCode | ||
} | ||
defer req.Body.Close() | ||
|
||
response := &openai.OpenAIProviderChatResponse{} | ||
// 发送请求 | ||
_, errWithCode = p.Requester.SendRequest(req, response, false) | ||
if errWithCode != nil { | ||
return nil, errWithCode | ||
} | ||
|
||
// 检测是否错误 | ||
openaiErr := openai.ErrorHandle(&response.OpenAIErrorResponse) | ||
if openaiErr != nil { | ||
errWithCode = &types.OpenAIErrorWithStatusCode{ | ||
OpenAIError: *openaiErr, | ||
StatusCode: http.StatusBadRequest, | ||
} | ||
return nil, errWithCode | ||
} | ||
|
||
*p.Usage = *response.Usage | ||
|
||
return &response.ChatCompletionResponse, nil | ||
} | ||
|
||
func (p *GroqProvider) CreateChatCompletionStream(request *types.ChatCompletionRequest) (requester.StreamReaderInterface[string], *types.OpenAIErrorWithStatusCode) { | ||
p.getChatRequestBody(request) | ||
req, errWithCode := p.GetRequestTextBody(common.RelayModeChatCompletions, request.Model, request) | ||
if errWithCode != nil { | ||
return nil, errWithCode | ||
} | ||
defer req.Body.Close() | ||
|
||
// 发送请求 | ||
resp, errWithCode := p.Requester.SendRequestRaw(req) | ||
if errWithCode != nil { | ||
return nil, errWithCode | ||
} | ||
|
||
chatHandler := openai.OpenAIStreamHandler{ | ||
Usage: p.Usage, | ||
ModelName: request.Model, | ||
} | ||
|
||
return requester.RequestStream[string](p.Requester, resp, chatHandler.HandlerChatStream) | ||
} | ||
|
||
// 获取聊天请求体 | ||
func (p *GroqProvider) getChatRequestBody(request *types.ChatCompletionRequest) { | ||
if request.Tools != nil { | ||
request.Tools = nil | ||
} | ||
|
||
if request.ToolChoice != nil { | ||
request.ToolChoice = nil | ||
} | ||
|
||
if request.ResponseFormat != nil { | ||
request.ResponseFormat = nil | ||
} | ||
|
||
if request.N > 1 { | ||
request.N = 1 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters