forked from kubeagi/arcadia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubeagi#57 from Lanture1064/dev
refactor: Extract LLM validate logic & add ZhiPuAI validator
- Loading branch information
Showing
6 changed files
with
289 additions
and
70 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2023 KubeAGI. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package openai | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
OpenaiModelAPIURL = "https://api.openai.com/v1" | ||
OpenaiDefaultTimeout = 300 * time.Second | ||
) | ||
|
||
type OpenAI struct { | ||
apiKey string | ||
} | ||
|
||
func NewOpenAI(auth string) *OpenAI { | ||
return &OpenAI{ | ||
apiKey: auth, | ||
} | ||
} | ||
|
||
func (o *OpenAI) Validate() (*Response, error) { | ||
// Validate OpenAI type CRD LLM Instance | ||
// instance.Spec.URL should be like "https://api.openai.com/" | ||
|
||
if o.apiKey == "" { | ||
// TODO: maybe we should consider local pseudo-openAI LLM worker that doesn't require an apiKey? | ||
return nil, fmt.Errorf("auth is empty") | ||
} | ||
|
||
testURL := OpenaiModelAPIURL + "/models" | ||
testAuth := "Bearer " + o.apiKey // openAI official requirement | ||
|
||
req, err := http.NewRequest("GET", testURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Authorization", testAuth) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
cli := &http.Client{} | ||
resp, err := cli.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("returns unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
// FIXME: response object | ||
response, err := parseHTTPResponse(resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return response, nil | ||
} | ||
|
||
// TODO: Openai Model Object & Other definition |
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,75 @@ | ||
/* | ||
Copyright 2023 KubeAGI. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package openai | ||
|
||
// Chat is a chat completion response returned by model. | ||
type Chat struct { | ||
ID string `json:"id"` // A unique identifier for the chat completion | ||
Object string `json:"object"` // The object type, which is always chat.completion | ||
Created int `json:"created"` // A unix timestamp of when the chat completion was created. | ||
Model string `json:"model"` // The model used for the chat completion. | ||
Choices []Choice `json:"choices"` // A list of chat completion choices. Can be more than one if n is greater than 1. | ||
Usage Usage `json:"usage"` // Usage statistics of the completion request. | ||
} | ||
|
||
// ChatStream is a streamed chunk of a chat completion returned by model. | ||
type ChatStream struct { | ||
ID string `json:"id"` // A unique identifier for the chat completion. | ||
Object string `json:"object"` // The object type, which is always chat.completion | ||
Created int `json:"created"` // A unix timestamp of when the chat completion was created. | ||
Model string `json:"model"` // The model used for the chat completion. | ||
Choices []ChoiceStream `json:"choices"` // A list of chat completion choices. Can be more than one if n is greater than 1. | ||
} | ||
|
||
type Choice struct { | ||
Index int `json:"index"` // The index of the choice in the list of choices. | ||
Message Message `json:"message"` // The completion message generated by the model. | ||
FinishReason string `json:"finish_reason"` // The reason the model stopped generating tokens. This will be stop if the model hit a natural stop point or a provided stop sequence, `length` if the maximum number of tokens specified in the request was reached, or `function_call` if the model called a function. | ||
} | ||
|
||
type ChoiceStream struct { | ||
Index int `json:"index"` | ||
Delta Delta `json:"delta"` | ||
FinishReason string `json:"finish_reason"` | ||
} | ||
|
||
// Message is a chat completion message generated by the model. | ||
type Message struct { | ||
Role string `json:"role"` | ||
Content string `json:"content,omitempty"` | ||
FunctionCall FunctionCall `json:"function_call,omitempty"` | ||
} | ||
|
||
// FunctionCall is used when a message is calling a function generated by openAI model. | ||
type FunctionCall struct { | ||
Name string `json:"name"` // Name of the function. | ||
Arguments string `json:"arguments"` // JSON format of the arguments. | ||
} | ||
|
||
// Usage is the usage statistics of the completion request. | ||
type Usage struct { | ||
PromptTokens int `json:"prompt_tokens"` | ||
CompletionTokens int `json:"completion_tokens"` | ||
TotalTokens int `json:"total_tokens"` | ||
} | ||
|
||
// Delta is A chat completion delta generated by streamed model responses. | ||
type Delta struct { | ||
Role string `json:"role"` | ||
Content string `json:"content,omitempty"` | ||
FunctionCall FunctionCall `json:"function_call,omitempty"` | ||
} |
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,61 @@ | ||
/* | ||
Copyright 2023 KubeAGI. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package openai | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/kubeagi/arcadia/pkg/llms" | ||
"net/http" | ||
) | ||
|
||
type Response struct { | ||
Code int `json:"code"` | ||
Data string `json:"data"` // JSON format of the returned data | ||
Msg string `json:"msg"` | ||
Success bool `json:"success"` | ||
} | ||
|
||
func (response *Response) Type() llms.LLMType { | ||
return llms.OpenAI | ||
} | ||
|
||
func (response *Response) Bytes() []byte { | ||
bytes, err := json.Marshal(response) | ||
if err != nil { | ||
return []byte{} | ||
} | ||
return bytes | ||
} | ||
|
||
func (response *Response) String() string { | ||
return string(response.Bytes()) | ||
} | ||
|
||
func parseHTTPResponse(resp *http.Response) (*Response, error) { | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("exception: %s", resp.Status) | ||
} | ||
|
||
var data = new(Response) | ||
err := json.NewDecoder(resp.Body).Decode(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |
Oops, something went wrong.