-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extract LLM validate logic
Signed-off-by: Lanture1064 <[email protected]>
- Loading branch information
1 parent
90448ef
commit 6d2292a
Showing
4 changed files
with
137 additions
and
57 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
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,72 @@ | ||
/* | ||
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 ( | ||
OPENAI_MODEL_API_URL = "https://api.openai.com/v1" | ||
OPENAI_MODEL_Default_Timeout = 300 * time.Second | ||
) | ||
|
||
type OpenAI struct { | ||
apiKey string | ||
} | ||
|
||
func NewOpenAI(auth string) *OpenAI { | ||
return &OpenAI{ | ||
apiKey: auth, | ||
} | ||
} | ||
|
||
func (o *OpenAI) Validate() (string, 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 "", fmt.Errorf("auth is empty") | ||
} | ||
|
||
testURL := OPENAI_MODEL_API_URL + "/models" | ||
testAuth := "Bearer " + o.apiKey // openAI official requirement | ||
|
||
req, err := http.NewRequest("GET", testURL, nil) | ||
if err != nil { | ||
return "", 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 "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("returns unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
return "GET model request returns Status OK", nil | ||
} |
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