如何通过api的方式调用?
394988736 opened this issue · 4 comments
394988736 commented
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// 替换为你的 Access Token 和目标 URL
string accessToken = "";
string url = "http://xx:8899/api/models";
// 创建 HttpClient 实例
using (HttpClient client = new HttpClient())
{
// 设置请求头中的 Authorization,添加 Access Token
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
try
{
// 发起 GET 请求
HttpResponseMessage response = await client.GetAsync(url);
// 检查响应状态码
if (response.IsSuccessStatusCode)
{
// 读取响应内容
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("响应内容:");
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("请求失败,状态码:" + response.StatusCode);
}
}
catch (Exception ex)
{
Console.WriteLine("发生异常:" + ex.Message);
}
}
}
}
为什么这样访问不到的呢?
undefinedcodezhong commented
我本地测了下好像可以呀:
In [1]: import requests
In [2]: response = requests.get('http://127.0.0.1:1234/api/models')
In [3]: response.content
Out[4]: b'{"models":[{"slug":"text-davinci-002-render-sha","max_tokens":8191,"title":"Default (GPT-3.5)","description":"Our fastest model, great for most everyday tasks.","tags":["gpt3.5"],"capabilities":{},"product_features":{}}],"categories":[{"category":"gpt_3.5","human_category_name":"GPT-3.5","subscription_level":"free","default_model":"text-davinci-002-render-sha","browsing_model":"text-davinci-002-render-sha-browsing","code_interpreter_model":"text-davinci-002-render-sha-code-interpreter","plugins_model":"text-davinci-002-render-sha-plugins"}]}'
394988736 commented
你的 requests是自己封装的吗,啥写的?
undefinedcodezhong commented
就是用的python默认的requests库,可以很方便发起http请求
xiangyingchang commented
string accessToken
请问这个是直接设置环境变量就可以调用吗?我是用 docker 本地