
開發者生產力提升的API終極指南
#include <curl/curl.h>
#include <iostream>
#include <string>
int main() {
CURL *curl;
CURLcode res;
std::string readBuffer;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
std::string url = "https://api.explinks.com/v2/scd202407082255150d8801/medgpt-ai-doctor";
// 設置API端點
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// 設置請求頭,例如API密鑰
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
std::string apiKey = "Your-API-Key-Here";
headers = curl_slist_append(headers, ("X-API-KEY: " + apiKey).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// 假設是POST請求,設置POST字段
std::string postData = "{\"patientInfo\": \"...\"}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
// 設置回調函數處理響應數據
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// 執行請求
res = curl_easy_perform(curl);
// 檢查錯誤
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
// 輸出結果
std::cout << "API Response: " << readBuffer << std::endl;
// 清理
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
curl_global_cleanup();
return 0;
}
size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *s) {
size_t newLength = size * nmemb;
try {
s->append((char*)contents, newLength);
} catch(std::bad_alloc &e) {
// handle memory problem
return 0;
}
return newLength;
}
// PHP代碼示例,展示如何調用MedGPT AI醫生API
<?php
$url = "https://api.explinks.com/v2/scd202407082255150d8801/medgpt-ai-doctor";
$apiKey = "Your-API-Key-Here";
$postData = array('patientInfo' => '...');
$jsonData = json_encode($postData);
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-API-KEY: ' . $apiKey
),
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (!$response) {
echo "cURL error: " . curl_error($ch);
} else {
echo "API Response: " . $response;
}
curl_close($ch);
?>
// Go代碼示例,展示如何調用MedGPT AI醫生API
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
url := "https://api.explinks.com/v2/scd202407082255150d8801/medgpt-ai-doctor"
apiKey := "Your-API-Key-Here"
patientInfo := map[string]string{"patientInfo": "..."}
jsonData, _ := json.Marshal(patientInfo)
request, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-API-KEY", apiKey)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("API request error:", err)
return
}
defer response.Body.Close()
responseBody, _ := ioutil.ReadAll(response.Body)
fmt.Println("API Response:", string(responseBody))
}
在開源社區中,有幾個項目提供了醫療AI API的解決方案,其中包括:
下面給出DoctorGLM的Python集成示例:
# 導入所需的庫
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# 加載模型和分詞器
model_name = "xionghonglin/DoctorGLM" # 模型名,根據DoctorGLM的官方模型名填寫
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# 定義一個函數來獲取模型的回答
def get_doctorglm_response(query):
# 編碼問題
inputs = tokenizer.encode(query, return_tensors="pt")
# 生成回答
response = model.generate(inputs, max_length=512, num_return_sequences=1)
# 解碼回答
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
return response_text
# 用戶輸入的問題
user_question = "我爺爺高血壓可以喝咖啡嗎"
# 獲取并打印AI的回答
ai_response = get_doctorglm_response(user_question)
print(f"AI的回答是: {ai_response}")
冪簡集成是國內領先的API集成管理平臺,專注于為開發者提供全面、高效、易用的API集成解決方案。冪簡API平臺可以通過以下兩種方式找到所需API:通過關鍵詞搜索API(例如,輸入’AI醫生‘這類品類詞,更容易找到結果)、或者從API Hub分類頁進入尋找。
此外,冪簡集成博客會編寫API入門指南、多語言API對接指南、API測評等維度的文章,讓開發者快速使用目標API。