在国内如何使用某知名GPT,附在线体验链接

发布时间:2023年12月21日

在线体验

在这里插入图片描述

三种方式:
一、访问openai,之前需要国外手机号,现在好像不用验证了,但是调用API还是需要验证手机号
二、购买一个海外服务器,然后自行在海外服务器上部署一套调用OpenAI接口的服务
三、通过一些国内封装好的网站访问,一般收取昂贵会员费

笔者采用的是方案二的改良版,在海外服务器上部署一套桥接服务,然后通过国内服务器进行调用
下面是部分代码:

public void streamCompletions(HttpServletResponse response, List<OpenAIResponsiveAPI.Message> messages, Float temperature) {
        OpenAiStreamApi.completionsStream(messages, temperature).blockingForEach(chunk -> {
            if (response.containsHeader("Content-Type")) {
                response.setHeader("Content-Type", "text/event-stream");
            } else {
                response.setHeader("Content-Type", "text/event-stream");
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Connection", "keep-alive");
            }
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("data: " + chunk);
            response.getWriter().flush();
            try {
                Thread.sleep(10);
            } catch (Exception ex) {
            }
        });
}

public abstract class StreamApiUtil {

    protected static final ObjectMapper mapper = defaultObjectMapper();

    public static ObjectMapper defaultObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        return mapper;
    }

    public static <T> Flowable<T> stream(Call<ResponseBody> apiCall, Class<T> cl) {
        return stream(apiCall).map(sse -> {
            T t = mapper.readValue(sse.getData(), cl);
            return t;
        });
    }

    public static Flowable<SSE> stream(Call<ResponseBody> apiCall) {
        return stream(apiCall, false);
    }

    public static Flowable<SSE> stream(Call<ResponseBody> apiCall, boolean emitDone) {
        return Flowable.create(emitter -> apiCall.enqueue(new ResponseBodyCallback(emitter, emitDone)), BackpressureStrategy.BUFFER);
    }

}

public class OpenAiUtil extends StreamApiUtil {
    public static Flowable<OpenAiResult> completionsStream(List<Message> messages, float temperature) {
        Map<String, Object> params = new HashMap<>();
        params.put("messages", messages);
        params.put("temperature", temperature);
        return stream(OPEN_AI_API.createCompletionStream(params), OpenAiResult.class);
    }
}

public interface OpenAiApi {

    @Streaming
    @POST("/api/chat-ai/completionsStream")
    Call<ResponseBody> createCompletionStream(@Body Map request);

}

点击在线体验

文章来源:https://blog.csdn.net/qq_27574367/article/details/135140327
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。