linshu64 commented on issue #1950: URL: https://github.com/apache/shiro/issues/1950#issuecomment-2622070927
> package io.renren.modules.stu.controller; > > import com.alibaba.dashscope.app.Application; import com.alibaba.dashscope.app.ApplicationParam; import com.alibaba.dashscope.app.ApplicationResult; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import io.reactivex.Flowable; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; > > import javax.servlet.http.HttpServletResponse; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; > > [@RequestMapping](https://github.com/RequestMapping)("stu/bot") [@RestController](https://github.com/RestController) public class StuBotController { > > ``` > @Value("${ai.api.appId}") > private String appId; > > /** > * 创建线程池 > **/ > ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 60, TimeUnit.SECONDS, > new LinkedBlockingQueue<>(10), > Executors.defaultThreadFactory(), > new ThreadPoolExecutor.AbortPolicy() > ); > > public void streamCall(SseEmitter emitter, String query) throws NoApiKeyException, InputRequiredException { > // appId 填入百炼应用 ID > ApplicationParam param = ApplicationParam.builder() > .apiKey(System.getenv("64b280a2b91c4775b37f74cbd7ea8eed")) > .appId(appId) > .prompt(query) > .incrementalOutput(false) > .build(); > > Application application = new Application(); > Flowable<ApplicationResult> result = application.streamCall(param); > AtomicInteger counter = new AtomicInteger(0); > result.blockingForEach(data -> { > int newValue = counter.incrementAndGet(); > String resData = "\nevent:result\n:HTTP_STATUS/200\ndata:" + new Gson().toJson(data); > emitter.send(resData, MediaType.APPLICATION_JSON); > > if ("stop".equals(data.getOutput().getFinishReason())) { > emitter.complete(); > } > }); > > } > > @RequestMapping(value = "/chat", method = RequestMethod.POST) > public SseEmitter streamData(@RequestBody String query, HttpServletResponse response) { > response.setContentType("text/event-stream;charset=UTF-8"); > response.setCharacterEncoding("UTF-8"); > SseEmitter emitter = new SseEmitter(180000L); > > executor.execute(() -> { > try { > JsonObject jsonObject = new JsonParser().parse(query).getAsJsonObject(); > streamCall(emitter, jsonObject.get("prompt").getAsString()); > } catch (NoApiKeyException | InputRequiredException e) { > e.printStackTrace(); > emitter.completeWithError(e); > } > }); > return emitter; > } > ``` > > } -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
