测试调用示例
Python 接入示例
import openai
client = openai.OpenAI( api_key="申请到的key", base_url="[hostname]/v1/")
response = client.chat.completions.create( model="模型ID", # model to send to the proxy messages=[{"role": "user", "content": "Hello World"}],)print(response)CURL 接入示例
curl --request GET \ --url [hostname]/v1/models \ --header 'authorization: Bearer 申请到的key' \ --header 'content-type: application/json'推理
curl --request POST \ --url [hostname]/chat/completions \ --header 'authorization: Bearer 申请到的key' \ --header 'content-type: application/json' \ --data '{ "model": "模型ID", "messages": [ { "role": "user", "content": "Hello World" } ]}'Golang 接入示例
package main
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http")
func main() { url := "[hostname]/v1/chat/completions"
// Define and marshal the payload payload, _ := json.Marshal(map[string]interface{}{ "model": "模型ID", "messages": []map[string]string{{ "role": "user", "content": "Hello World", }}, "stream": false, "max_tokens": 512, "temperature": 0.6, })
// Create and send the request req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload)) req.Header.Set("Authorization", "Bearer 申请的api-key") req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req) if err != nil { fmt.Println("Error:", err) return } defer res.Body.Close()
// Read and print the response body, _ := ioutil.ReadAll(res.Body) fmt.Println("Response:", string(body))}Java 接入示例
import com.mashape.unirest.http.HttpResponse;import com.mashape.unirest.http.Unirest;import org.json.JSONObject;import org.json.JSONArray;
public class Test { public static void main(String[] args) throws Exception { JSONObject body = new JSONObject() .put("model", "模型ID") .put("messages", new JSONArray().put(new JSONObject().put("role", "user").put("content", "Hello World"))) .put("stream", false) .put("max_tokens", 512) .put("temperature", 0.6);
HttpResponse<String> response = Unirest.post("[hostname]/v1/chat/completions") .header("Authorization", "Bearer 申请的api-key") .header("Content-Type", "application/json") .body(body.toString()) .asString();
System.out.println(response.getBody()); }}