74 changed files with 1188 additions and 400 deletions
@ -1,4 +0,0 @@ |
|||||
package io.sc.platform.ai.anythingllm.service; |
|
||||
|
|
||||
public interface AnythingllmService { |
|
||||
} |
|
@ -1,6 +0,0 @@ |
|||||
package io.sc.platform.ai.anythingllm.service.impl; |
|
||||
|
|
||||
import io.sc.platform.ai.anythingllm.service.AnythingllmService; |
|
||||
|
|
||||
public class AnythingllmServiceImpl implements AnythingllmService { |
|
||||
} |
|
@ -1,86 +0,0 @@ |
|||||
package io.sc.platform.ai.anythingllm.service.support.workspaces; |
|
||||
|
|
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
|
||||
import io.sc.platform.ai.ollama.MessageWrapper; |
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
import io.sc.platform.ai.ollama.service.support.chat.ChatRequest; |
|
||||
import io.sc.platform.ai.ollama.service.support.chat.Message; |
|
||||
import io.sc.platform.util.CollectionUtil; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
import okhttp3.*; |
|
||||
import org.slf4j.Logger; |
|
||||
import org.slf4j.LoggerFactory; |
|
||||
import org.springframework.util.StringUtils; |
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
public class StreamChatApi extends OllamaApi { |
|
||||
private static final Logger log = LoggerFactory.getLogger(StreamChatApi.class); |
|
||||
|
|
||||
public StreamChatApi(String baseUrl){ |
|
||||
super("/api/chat","POST"); |
|
||||
this.baseUrl =baseUrl; |
|
||||
} |
|
||||
|
|
||||
public ResponseBodyEmitter execute(MessageWrapper chatRequest) { |
|
||||
ChatRequest chatCompletion =createChatCompletion(chatRequest); |
|
||||
if(chatCompletion==null) { return null; } |
|
||||
Call call =createRequestCall(chatCompletion); |
|
||||
if(call==null) { return null; } |
|
||||
|
|
||||
ResponseBodyEmitter emitter =new ResponseBodyEmitter(); |
|
||||
call.enqueue(new StreamChatCallback(emitter)); |
|
||||
return emitter; |
|
||||
} |
|
||||
|
|
||||
private ChatRequest createChatCompletion(MessageWrapper chatRequest) { |
|
||||
if(chatRequest==null) { return null; } |
|
||||
|
|
||||
String model =chatRequest.getModel(); |
|
||||
if(!StringUtils.hasText(model)) { return null; } |
|
||||
|
|
||||
List<String> questions =chatRequest.getQuestions(); |
|
||||
if(!CollectionUtil.hasElements(questions)){ return null; } |
|
||||
|
|
||||
ChatRequest chatCompletion =new ChatRequest(); |
|
||||
chatCompletion.setModel(model); |
|
||||
|
|
||||
chatCompletion.addMessage(new Message("user", "使用中文回答")); |
|
||||
for(String question : questions){ |
|
||||
chatCompletion.addMessage(new Message("user",question)); |
|
||||
} |
|
||||
return chatCompletion; |
|
||||
} |
|
||||
|
|
||||
private Call createRequestCall(ChatRequest chatCompletion) { |
|
||||
if(chatCompletion==null) { return null; } |
|
||||
|
|
||||
OkHttpClient client = new OkHttpClient.Builder() |
|
||||
.connectTimeout(this.connectTimeout) |
|
||||
.readTimeout(this.readTimeout) |
|
||||
.writeTimeout(this.writeTimeout) |
|
||||
.build(); |
|
||||
|
|
||||
Headers headers = new Headers.Builder() |
|
||||
.set("Content-Type", "application/json") |
|
||||
.set("Accept", "text/event-stream") |
|
||||
.build(); |
|
||||
String json =""; |
|
||||
try { |
|
||||
json = ObjectMapperUtil.json().writeValueAsString(chatCompletion); |
|
||||
} catch (JsonProcessingException e){ |
|
||||
log.error("",e); |
|
||||
return null; |
|
||||
} |
|
||||
RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8")); |
|
||||
okhttp3.Request request = new okhttp3.Request.Builder() |
|
||||
.url(this.baseUrl + this.url) |
|
||||
.headers(headers) |
|
||||
.post(body) |
|
||||
.build(); |
|
||||
|
|
||||
Call call = client.newCall(request); |
|
||||
return call; |
|
||||
} |
|
||||
} |
|
@ -1,29 +1,20 @@ |
|||||
package io.sc.platform.ai.ollama; |
package io.sc.platform.ai.api; |
||||
|
|
||||
import java.time.Duration; |
import java.time.Duration; |
||||
|
|
||||
public class OllamaApi { |
public class AiApi { |
||||
protected String baseUrl ="http://localhost:11434"; |
|
||||
protected String url; |
protected String url; |
||||
protected String method; |
protected String method; |
||||
protected Duration connectTimeout =Duration.ofMinutes(2); |
protected Duration connectTimeout =Duration.ofMinutes(2); |
||||
protected Duration readTimeout =Duration.ofMinutes(2); |
protected Duration readTimeout =Duration.ofMinutes(2); |
||||
protected Duration writeTimeout =Duration.ofMinutes(2); |
protected Duration writeTimeout =Duration.ofMinutes(2); |
||||
|
|
||||
public OllamaApi(){} |
public AiApi(){} |
||||
public OllamaApi(String url,String method){ |
public AiApi(String url, String method){ |
||||
this.url =url; |
this.url =url; |
||||
this.method =method; |
this.method =method; |
||||
} |
} |
||||
|
|
||||
public String getBaseUrl() { |
|
||||
return baseUrl; |
|
||||
} |
|
||||
|
|
||||
public void setBaseUrl(String baseUrl) { |
|
||||
this.baseUrl = baseUrl; |
|
||||
} |
|
||||
|
|
||||
public String getUrl() { |
public String getUrl() { |
||||
return url; |
return url; |
||||
} |
} |
@ -0,0 +1,19 @@ |
|||||
|
package io.sc.platform.ai.api; |
||||
|
|
||||
|
/** |
||||
|
* 服务提供商名称枚举 |
||||
|
*/ |
||||
|
public enum ServiceProvider { |
||||
|
ANYTHING_LLM("AnythingLLM"), |
||||
|
OLLAMA("Ollama"); |
||||
|
|
||||
|
private String value; |
||||
|
|
||||
|
ServiceProvider(String value){ |
||||
|
this.value =value; |
||||
|
} |
||||
|
|
||||
|
public String value(){ |
||||
|
return this.value; |
||||
|
} |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package io.sc.platform.ai.api.controller; |
||||
|
|
||||
|
import io.sc.platform.ai.api.service.ApiService; |
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
@RestController("io.sc.platform.ai.api.controller.ApiWebController") |
||||
|
@RequestMapping("/api/ai") |
||||
|
public class ApiWebController { |
||||
|
@Autowired private ApiService apiService; |
||||
|
|
||||
|
@PostMapping("/chat") |
||||
|
public ResponseBodyEmitter chat(@RequestBody MessageWrapper wrapper){ |
||||
|
return apiService.chat(wrapper); |
||||
|
} |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
package io.sc.platform.ai.api.service; |
||||
|
|
||||
|
/** |
||||
|
* API 配置服务接口 |
||||
|
*/ |
||||
|
public interface ApiConfigurationService { |
||||
|
/** |
||||
|
* 获取默认服务提供商名称 |
||||
|
* @return 默认服务提供商名称 |
||||
|
*/ |
||||
|
public String getDefaultServiceProvider(); |
||||
|
|
||||
|
/** |
||||
|
* 获取 ollama api base url |
||||
|
* @return ollama api base url |
||||
|
*/ |
||||
|
public String getOllamaApiBaseUrl(); |
||||
|
|
||||
|
/** |
||||
|
* 获取 ollama 默认模型名称 |
||||
|
* @return ollama 默认模型名称 |
||||
|
*/ |
||||
|
public String getOllamaDefaultModelName(); |
||||
|
|
||||
|
/** |
||||
|
* 获取 AnythingLLM api base url |
||||
|
* @return AnythingLLM api base url |
||||
|
*/ |
||||
|
public String getAnythingllmApiBaseUrl(); |
||||
|
|
||||
|
/** |
||||
|
* 获取 AnythingLLM api 秘钥 |
||||
|
* @return AnythingLLM api 秘钥 |
||||
|
*/ |
||||
|
public String getAnythingllmApiKey(); |
||||
|
|
||||
|
/** |
||||
|
* 获取 AnythingLLM 默认工作空间 |
||||
|
* @return AnythingLLM 默认工作空间 |
||||
|
*/ |
||||
|
public String getAnythingllmDefaultWorkspace(); |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
package io.sc.platform.ai.api.service; |
||||
|
|
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
public interface ApiService { |
||||
|
public ResponseBodyEmitter chat(MessageWrapper wrapper); |
||||
|
} |
@ -0,0 +1,53 @@ |
|||||
|
package io.sc.platform.ai.api.service.impl; |
||||
|
|
||||
|
import io.sc.platform.ai.api.service.ApiConfigurationService; |
||||
|
import io.sc.platform.mvc.service.SystemParameterService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
@Service("io.sc.platform.ai.api.service.impl.ApiConfigurationServiceImpl") |
||||
|
public class ApiConfigurationServiceImpl implements ApiConfigurationService { |
||||
|
// default service provider
|
||||
|
private static final String KEY_DEFAULT_SERVICE_PROVIDER ="parameter.ai.provider.default"; |
||||
|
|
||||
|
// Ollama config
|
||||
|
private static final String KEY_OLLAMA_API_BASE_URL ="parameter.ai.provider.ollama.apiUrl"; |
||||
|
private static final String KEY_OLLAMA_DEFAULT_MODEL_NAME ="parameter.ai.provider.ollama.defaultModelName"; |
||||
|
|
||||
|
// AnythingLLM config
|
||||
|
private static final String KEY_ANYTHINGLLM_API_BASE_URL ="parameter.ai.provider.anythingllm.apiUrl"; |
||||
|
private static final String KEY_ANYTHINGLLM_API_KEY ="parameter.ai.provider.anythingllm.apiKey"; |
||||
|
private static final String KEY_ANYTHINGLLM_DEFAULT_WORKSPACE_KEY ="parameter.ai.provider.anythingllm.defaultWorkspace"; |
||||
|
|
||||
|
@Autowired private SystemParameterService systemParameterService; |
||||
|
|
||||
|
@Override |
||||
|
public String getDefaultServiceProvider() { |
||||
|
return systemParameterService.getParameter(KEY_DEFAULT_SERVICE_PROVIDER); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getOllamaApiBaseUrl() { |
||||
|
return systemParameterService.getParameter(KEY_OLLAMA_API_BASE_URL); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getOllamaDefaultModelName() { |
||||
|
return systemParameterService.getParameter(KEY_OLLAMA_DEFAULT_MODEL_NAME); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getAnythingllmApiBaseUrl() { |
||||
|
return systemParameterService.getParameter(KEY_ANYTHINGLLM_API_BASE_URL); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getAnythingllmApiKey() { |
||||
|
return systemParameterService.getParameter(KEY_ANYTHINGLLM_API_KEY); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getAnythingllmDefaultWorkspace() { |
||||
|
return systemParameterService.getParameter(KEY_ANYTHINGLLM_DEFAULT_WORKSPACE_KEY); |
||||
|
} |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
package io.sc.platform.ai.api.service.impl; |
||||
|
|
||||
|
import io.sc.platform.ai.api.ServiceProvider; |
||||
|
import io.sc.platform.ai.api.service.ApiConfigurationService; |
||||
|
import io.sc.platform.ai.api.service.ApiService; |
||||
|
import io.sc.platform.ai.provider.anythingllm.service.AnythingllmService; |
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import io.sc.platform.ai.provider.ollama.service.OllamaService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
@Service("io.sc.platform.ai.api.service.impl.ApiServiceImpl") |
||||
|
public class ApiServiceImpl implements ApiService { |
||||
|
@Autowired private ApiConfigurationService apiConfigurationService; |
||||
|
|
||||
|
@Autowired private AnythingllmService anythingllmService; |
||||
|
@Autowired private OllamaService ollamaService; |
||||
|
|
||||
|
@Override |
||||
|
public ResponseBodyEmitter chat(MessageWrapper wrapper) { |
||||
|
String provider =apiConfigurationService.getDefaultServiceProvider(); |
||||
|
if(ServiceProvider.ANYTHING_LLM.value().equals(provider)){ |
||||
|
return anythingllmService.chat(wrapper); |
||||
|
}else if(ServiceProvider.OLLAMA.value().equals(provider)){ |
||||
|
return ollamaService.chat(wrapper); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
@ -1,13 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.MessageWrapper; |
|
||||
import io.sc.platform.ai.ollama.service.support.tags.TagsResponse; |
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
public interface OllamaService { |
|
||||
public ResponseBodyEmitter chat(MessageWrapper wrapper); |
|
||||
public TagsResponse tags(); |
|
||||
public List<String> modelNames(); |
|
||||
} |
|
@ -1,69 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.impl; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.MessageWrapper; |
|
||||
import io.sc.platform.ai.ollama.service.OllamaService; |
|
||||
import io.sc.platform.ai.ollama.service.support.chat.ChatApi; |
|
||||
import io.sc.platform.ai.ollama.service.support.tags.Model; |
|
||||
import io.sc.platform.ai.ollama.service.support.tags.TagsApi; |
|
||||
import io.sc.platform.ai.ollama.service.support.tags.TagsResponse; |
|
||||
import io.sc.platform.mvc.service.SystemParameterService; |
|
||||
import io.sc.platform.util.CollectionUtil; |
|
||||
import io.sc.platform.util.StringUtil; |
|
||||
import io.sc.platform.util.support.NumberStringComparator; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.Collections; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
@Service("io.sc.platform.ai.service.impl.OllamaServiceImpl") |
|
||||
public class OllamaServiceImpl implements OllamaService { |
|
||||
private static final String KEY_API_URL ="parameter.ai.provider.ollama.apiUrl"; |
|
||||
private static final String KEY_DEFAULT_MODEL_NAME ="parameter.ai.provider.ollama.defaultModelName"; |
|
||||
|
|
||||
@Autowired private SystemParameterService systemParameterService; |
|
||||
|
|
||||
@Override |
|
||||
public ResponseBodyEmitter chat(MessageWrapper wrapper) { |
|
||||
Map<String,String> parameters =getParameters(); |
|
||||
if(!StringUtil.hasText(wrapper.getModel())){ |
|
||||
wrapper.setModel(parameters.get(KEY_DEFAULT_MODEL_NAME)); |
|
||||
} |
|
||||
ChatApi api =new ChatApi(parameters.get(KEY_API_URL)); |
|
||||
return api.execute(wrapper); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public TagsResponse tags() { |
|
||||
Map<String,String> parameters =getParameters(); |
|
||||
TagsApi api =new TagsApi(parameters.get(KEY_API_URL)); |
|
||||
return api.execute(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public List<String> modelNames() { |
|
||||
Map<String,String> parameters =getParameters(); |
|
||||
TagsApi api =new TagsApi(parameters.get(KEY_API_URL)); |
|
||||
TagsResponse response =api.execute(); |
|
||||
if(response==null){ |
|
||||
return Collections.emptyList(); |
|
||||
} |
|
||||
List<Model> models =response.getModels(); |
|
||||
if(!CollectionUtil.hasElements(models)){ |
|
||||
return Collections.emptyList(); |
|
||||
} |
|
||||
List<String> result =new ArrayList<>(); |
|
||||
for(Model model : models){ |
|
||||
result.add(model.getModel()); |
|
||||
} |
|
||||
Collections.sort(result, new NumberStringComparator()); |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
private Map<String,String> getParameters(){ |
|
||||
return systemParameterService.getParameters(new String[]{KEY_API_URL,KEY_DEFAULT_MODEL_NAME}); |
|
||||
} |
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.blobs; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class BlobsApi extends OllamaApi { |
|
||||
public BlobsApi(){ |
|
||||
super("/api/blobs/","HEAD"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,4 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.chat; |
|
||||
|
|
||||
public class Tool { |
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.copy; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaCopyApi extends OllamaApi { |
|
||||
public OllamaCopyApi(){ |
|
||||
super("/api/copy","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.create; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaCreateApi extends OllamaApi { |
|
||||
public OllamaCreateApi(){ |
|
||||
super("/api/create","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.delete; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaDeleteApi extends OllamaApi { |
|
||||
public OllamaDeleteApi(){ |
|
||||
super("/api/delete","DELETE"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.embed; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaEmbedApi extends OllamaApi { |
|
||||
public OllamaEmbedApi(){ |
|
||||
super("/api/embed","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.embeddings; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaEmbeddingsApi extends OllamaApi { |
|
||||
public OllamaEmbeddingsApi(){ |
|
||||
super("/api/embeddings","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.generate; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaGenerateApi extends OllamaApi { |
|
||||
public OllamaGenerateApi(){ |
|
||||
super("/api/generate","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.ps; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaPsApi extends OllamaApi { |
|
||||
public OllamaPsApi(){ |
|
||||
super("/api/ps","GET"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.pull; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaPullApi extends OllamaApi { |
|
||||
public OllamaPullApi(){ |
|
||||
super("/api/pull","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.push; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaPushApi extends OllamaApi { |
|
||||
public OllamaPushApi(){ |
|
||||
super("/api/push","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.show; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaShowApi extends OllamaApi { |
|
||||
public OllamaShowApi(){ |
|
||||
super("/api/show","POST"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.version; |
|
||||
|
|
||||
import io.sc.platform.ai.ollama.OllamaApi; |
|
||||
|
|
||||
public class OllamaVersionApi extends OllamaApi { |
|
||||
public OllamaVersionApi(){ |
|
||||
super("/api/version","GET"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
@ -0,0 +1,20 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.api; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
|
||||
|
public class AnythingllmApi extends AiApi { |
||||
|
protected String apiKey; |
||||
|
|
||||
|
public AnythingllmApi(String url,String method,String apiKey) { |
||||
|
super(url,method); |
||||
|
this.apiKey =apiKey; |
||||
|
} |
||||
|
|
||||
|
public String getApiKey() { |
||||
|
return apiKey; |
||||
|
} |
||||
|
|
||||
|
public void setApiKey(String apiKey) { |
||||
|
this.apiKey = apiKey; |
||||
|
} |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.service; |
||||
|
|
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface AnythingllmService { |
||||
|
public ResponseBodyEmitter chat(MessageWrapper wrapper); |
||||
|
|
||||
|
/** |
||||
|
* 获取所有工作空间名称列表 |
||||
|
* @return 所有工作空间名称列表 |
||||
|
*/ |
||||
|
public List<String> workspaceNames(); |
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.service.impl; |
||||
|
|
||||
|
import io.sc.platform.ai.api.service.ApiConfigurationService; |
||||
|
import io.sc.platform.ai.provider.anythingllm.service.AnythingllmService; |
||||
|
import io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat.ChatApi; |
||||
|
import io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces.WorkspacesApi; |
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Service("io.sc.platform.ai.provider.anythingllm.service.impl.AnythingllmServiceImpl") |
||||
|
public class AnythingllmServiceImpl implements AnythingllmService { |
||||
|
@Autowired private ApiConfigurationService apiConfigurationService; |
||||
|
|
||||
|
@Override |
||||
|
public ResponseBodyEmitter chat(MessageWrapper wrapper) { |
||||
|
String baseUrl =apiConfigurationService.getAnythingllmApiBaseUrl(); |
||||
|
String apiKey =apiConfigurationService.getAnythingllmApiKey(); |
||||
|
String defaultWorkspace=apiConfigurationService.getAnythingllmDefaultWorkspace(); |
||||
|
ChatApi api =new ChatApi(baseUrl,apiKey,defaultWorkspace); |
||||
|
return api.execute(wrapper); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<String> workspaceNames() { |
||||
|
String baseUrl =apiConfigurationService.getAnythingllmApiBaseUrl(); |
||||
|
String apiKey =apiConfigurationService.getAnythingllmApiKey(); |
||||
|
WorkspacesApi api =new WorkspacesApi(baseUrl,apiKey); |
||||
|
return api.workspaceNames(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,87 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import io.sc.platform.ai.provider.anythingllm.api.AnythingllmApi; |
||||
|
import io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat.request.ChatRequest; |
||||
|
import io.sc.platform.security.util.SecurityUtil; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
import okhttp3.*; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public class ChatApi extends AnythingllmApi { |
||||
|
private static final Logger log = LoggerFactory.getLogger(ChatApi.class); |
||||
|
|
||||
|
public ChatApi(String baseUrl, String apiKey,String workspace){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/v1/workspace/" + workspace + "/stream-chat"),"POST",apiKey); |
||||
|
this.apiKey =apiKey; |
||||
|
} |
||||
|
|
||||
|
public ResponseBodyEmitter execute(MessageWrapper wrapper) { |
||||
|
ChatRequest chatRequest =createChatRequest(wrapper); |
||||
|
if(chatRequest==null) { return null; } |
||||
|
Call call =createRequestCall(chatRequest); |
||||
|
if(call==null) { return null; } |
||||
|
|
||||
|
ResponseBodyEmitter emitter =new ResponseBodyEmitter(); |
||||
|
call.enqueue(new ChatCallback(emitter)); |
||||
|
return emitter; |
||||
|
} |
||||
|
|
||||
|
private ChatRequest createChatRequest(MessageWrapper wrapper) { |
||||
|
if(wrapper==null) { return null; } |
||||
|
|
||||
|
List<String> questions =wrapper.getQuestions(); |
||||
|
if(!CollectionUtil.hasElements(questions)){ return null; } |
||||
|
|
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
for(String question : questions){ |
||||
|
sb.append(question).append("\n"); |
||||
|
} |
||||
|
ChatRequest request =new ChatRequest(); |
||||
|
request.setMode("chat"); |
||||
|
request.setMessage(sb.toString()); |
||||
|
request.setSessionId(SecurityUtil.getUserId()); |
||||
|
return request; |
||||
|
} |
||||
|
|
||||
|
private Call createRequestCall(ChatRequest chatRequest) { |
||||
|
if(chatRequest==null) { return null; } |
||||
|
|
||||
|
OkHttpClient client = new OkHttpClient.Builder() |
||||
|
.connectTimeout(this.connectTimeout) |
||||
|
.readTimeout(this.readTimeout) |
||||
|
.writeTimeout(this.writeTimeout) |
||||
|
.build(); |
||||
|
|
||||
|
Headers headers = new Headers.Builder() |
||||
|
.add("Content-Type: application/json") |
||||
|
.add("Accept: text/event-stream") |
||||
|
.add("Authorization: Bearer " + apiKey) |
||||
|
.build(); |
||||
|
String json =""; |
||||
|
try { |
||||
|
json = ObjectMapperUtil.json().writeValueAsString(chatRequest); |
||||
|
} catch (JsonProcessingException e){ |
||||
|
log.error("",e); |
||||
|
return null; |
||||
|
} |
||||
|
RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8")); |
||||
|
log.info("[" + this.getMethod() + "]: " + this.url); |
||||
|
log.info(json); |
||||
|
okhttp3.Request request = new okhttp3.Request.Builder() |
||||
|
.url(this.url) |
||||
|
.headers(headers) |
||||
|
.post(body) |
||||
|
.build(); |
||||
|
|
||||
|
Call call = client.newCall(request); |
||||
|
return call; |
||||
|
} |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat.request; |
||||
|
|
||||
|
public class Attachment { |
||||
|
private String name; |
||||
|
private String mime; |
||||
|
private String contentString; |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public String getMime() { |
||||
|
return mime; |
||||
|
} |
||||
|
|
||||
|
public void setMime(String mime) { |
||||
|
this.mime = mime; |
||||
|
} |
||||
|
|
||||
|
public String getContentString() { |
||||
|
return contentString; |
||||
|
} |
||||
|
|
||||
|
public void setContentString(String contentString) { |
||||
|
this.contentString = contentString; |
||||
|
} |
||||
|
} |
@ -0,0 +1,46 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat.request; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class ChatRequest { |
||||
|
private String message; |
||||
|
private String mode; |
||||
|
private String sessionId; |
||||
|
private List<Attachment> attachments =new ArrayList<>(); |
||||
|
|
||||
|
public String getMessage() { |
||||
|
return message; |
||||
|
} |
||||
|
|
||||
|
public void setMessage(String message) { |
||||
|
this.message = message; |
||||
|
} |
||||
|
|
||||
|
public String getMode() { |
||||
|
return mode; |
||||
|
} |
||||
|
|
||||
|
public void setMode(String mode) { |
||||
|
this.mode = mode; |
||||
|
} |
||||
|
|
||||
|
public String getSessionId() { |
||||
|
return sessionId; |
||||
|
} |
||||
|
|
||||
|
public void setSessionId(String sessionId) { |
||||
|
this.sessionId = sessionId; |
||||
|
} |
||||
|
|
||||
|
public List<Attachment> getAttachments() { |
||||
|
return attachments; |
||||
|
} |
||||
|
|
||||
|
public void setAttachments(List<Attachment> attachments) { |
||||
|
this.attachments = attachments; |
||||
|
} |
||||
|
} |
@ -0,0 +1,64 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat.response; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class Message { |
||||
|
private String id; |
||||
|
private String type; |
||||
|
private String textResponse; |
||||
|
private List<Source> sources =new ArrayList<>(); |
||||
|
private boolean close; |
||||
|
private String error; |
||||
|
|
||||
|
public String getId() { |
||||
|
return id; |
||||
|
} |
||||
|
|
||||
|
public void setId(String id) { |
||||
|
this.id = id; |
||||
|
} |
||||
|
|
||||
|
public String getType() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public void setType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
public String getTextResponse() { |
||||
|
return textResponse; |
||||
|
} |
||||
|
|
||||
|
public void setTextResponse(String textResponse) { |
||||
|
this.textResponse = textResponse; |
||||
|
} |
||||
|
|
||||
|
public List<Source> getSources() { |
||||
|
return sources; |
||||
|
} |
||||
|
|
||||
|
public void setSources(List<Source> sources) { |
||||
|
this.sources = sources; |
||||
|
} |
||||
|
|
||||
|
public boolean isClose() { |
||||
|
return close; |
||||
|
} |
||||
|
|
||||
|
public void setClose(boolean close) { |
||||
|
this.close = close; |
||||
|
} |
||||
|
|
||||
|
public String getError() { |
||||
|
return error; |
||||
|
} |
||||
|
|
||||
|
public void setError(String error) { |
||||
|
this.error = error; |
||||
|
} |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.streamchat.response; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class Source { |
||||
|
} |
@ -0,0 +1,71 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces; |
||||
|
|
||||
|
import io.sc.platform.ai.provider.anythingllm.api.AnythingllmApi; |
||||
|
import io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces.response.Workspace; |
||||
|
import io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces.response.WorkspacesResponse; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
import okhttp3.*; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class WorkspacesApi extends AnythingllmApi { |
||||
|
|
||||
|
public WorkspacesApi(String baseUrl, String apiKey){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/v1/workspaces"),"GET",apiKey); |
||||
|
this.apiKey =apiKey; |
||||
|
} |
||||
|
|
||||
|
public WorkspacesResponse execute() { |
||||
|
Call call =createRequestCall(); |
||||
|
if(call==null) { return null; } |
||||
|
try { |
||||
|
Response response = call.execute(); |
||||
|
ResponseBody body =response.body(); |
||||
|
WorkspacesResponse workspacesResponse = ObjectMapperUtil.json().readValue(body.source().readUtf8(), WorkspacesResponse.class); |
||||
|
return workspacesResponse; |
||||
|
} catch (IOException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public List<String> workspaceNames() { |
||||
|
WorkspacesResponse workspacesResponse =execute(); |
||||
|
if(workspacesResponse==null) { return Collections.emptyList(); } |
||||
|
List<Workspace> workspaces =workspacesResponse.getWorkspaces(); |
||||
|
if(!CollectionUtil.hasElements(workspaces)) { return Collections.emptyList(); } |
||||
|
List<String> result =new ArrayList<>(); |
||||
|
for(Workspace workspace : workspacesResponse.getWorkspaces()){ |
||||
|
result.add(workspace.getName()); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
private Call createRequestCall() { |
||||
|
OkHttpClient client = new OkHttpClient.Builder() |
||||
|
.connectTimeout(this.connectTimeout) |
||||
|
.readTimeout(this.readTimeout) |
||||
|
.writeTimeout(this.writeTimeout) |
||||
|
.build(); |
||||
|
Request request = new Request.Builder() |
||||
|
.url(this.url) |
||||
|
.header("Authorization","Bearer " + apiKey) |
||||
|
.get() |
||||
|
.build(); |
||||
|
|
||||
|
Call call = client.newCall(request); |
||||
|
return call; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
WorkspacesApi api =new WorkspacesApi("http://localhost:3001","8X0N9TN-HQ3MTVK-M4NM8BQ-W6EYE8A"); |
||||
|
WorkspacesResponse workspacesResponse =api.execute(); |
||||
|
for(Workspace workspace : workspacesResponse.getWorkspaces()){ |
||||
|
System.out.println(workspace.getName()); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces.response; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class Thread { |
||||
|
@JsonProperty("user_id") |
||||
|
private String userId; |
||||
|
|
||||
|
private String slug; |
||||
|
private String name; |
||||
|
|
||||
|
public String getUserId() { |
||||
|
return userId; |
||||
|
} |
||||
|
|
||||
|
public void setUserId(String userId) { |
||||
|
this.userId = userId; |
||||
|
} |
||||
|
|
||||
|
public String getSlug() { |
||||
|
return slug; |
||||
|
} |
||||
|
|
||||
|
public void setSlug(String slug) { |
||||
|
this.slug = slug; |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
} |
@ -0,0 +1,189 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces.response; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class Workspace { |
||||
|
private long id; |
||||
|
private String name; |
||||
|
private String slug; |
||||
|
private String vectorTag; |
||||
|
private String createdAt; |
||||
|
private String openAiTemp; |
||||
|
private long openAiHistory; |
||||
|
private String lastUpdatedAt; |
||||
|
private String openAiPrompt; |
||||
|
private double similarityThreshold; |
||||
|
private String chatProvider; |
||||
|
private String chatModel; |
||||
|
private int topN; |
||||
|
private String chatMode; |
||||
|
private String pfpFilename; |
||||
|
private String agentProvider; |
||||
|
private String agentModel; |
||||
|
private String queryRefusalResponse; |
||||
|
private String vectorSearchMode; |
||||
|
private List<Thread> threads; |
||||
|
|
||||
|
public long getId() { |
||||
|
return id; |
||||
|
} |
||||
|
|
||||
|
public void setId(long id) { |
||||
|
this.id = id; |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public String getSlug() { |
||||
|
return slug; |
||||
|
} |
||||
|
|
||||
|
public void setSlug(String slug) { |
||||
|
this.slug = slug; |
||||
|
} |
||||
|
|
||||
|
public String getVectorTag() { |
||||
|
return vectorTag; |
||||
|
} |
||||
|
|
||||
|
public void setVectorTag(String vectorTag) { |
||||
|
this.vectorTag = vectorTag; |
||||
|
} |
||||
|
|
||||
|
public String getCreatedAt() { |
||||
|
return createdAt; |
||||
|
} |
||||
|
|
||||
|
public void setCreatedAt(String createdAt) { |
||||
|
this.createdAt = createdAt; |
||||
|
} |
||||
|
|
||||
|
public String getOpenAiTemp() { |
||||
|
return openAiTemp; |
||||
|
} |
||||
|
|
||||
|
public void setOpenAiTemp(String openAiTemp) { |
||||
|
this.openAiTemp = openAiTemp; |
||||
|
} |
||||
|
|
||||
|
public long getOpenAiHistory() { |
||||
|
return openAiHistory; |
||||
|
} |
||||
|
|
||||
|
public void setOpenAiHistory(long openAiHistory) { |
||||
|
this.openAiHistory = openAiHistory; |
||||
|
} |
||||
|
|
||||
|
public String getLastUpdatedAt() { |
||||
|
return lastUpdatedAt; |
||||
|
} |
||||
|
|
||||
|
public void setLastUpdatedAt(String lastUpdatedAt) { |
||||
|
this.lastUpdatedAt = lastUpdatedAt; |
||||
|
} |
||||
|
|
||||
|
public String getOpenAiPrompt() { |
||||
|
return openAiPrompt; |
||||
|
} |
||||
|
|
||||
|
public void setOpenAiPrompt(String openAiPrompt) { |
||||
|
this.openAiPrompt = openAiPrompt; |
||||
|
} |
||||
|
|
||||
|
public double getSimilarityThreshold() { |
||||
|
return similarityThreshold; |
||||
|
} |
||||
|
|
||||
|
public void setSimilarityThreshold(double similarityThreshold) { |
||||
|
this.similarityThreshold = similarityThreshold; |
||||
|
} |
||||
|
|
||||
|
public String getChatProvider() { |
||||
|
return chatProvider; |
||||
|
} |
||||
|
|
||||
|
public void setChatProvider(String chatProvider) { |
||||
|
this.chatProvider = chatProvider; |
||||
|
} |
||||
|
|
||||
|
public String getChatModel() { |
||||
|
return chatModel; |
||||
|
} |
||||
|
|
||||
|
public void setChatModel(String chatModel) { |
||||
|
this.chatModel = chatModel; |
||||
|
} |
||||
|
|
||||
|
public int getTopN() { |
||||
|
return topN; |
||||
|
} |
||||
|
|
||||
|
public void setTopN(int topN) { |
||||
|
this.topN = topN; |
||||
|
} |
||||
|
|
||||
|
public String getChatMode() { |
||||
|
return chatMode; |
||||
|
} |
||||
|
|
||||
|
public void setChatMode(String chatMode) { |
||||
|
this.chatMode = chatMode; |
||||
|
} |
||||
|
|
||||
|
public String getPfpFilename() { |
||||
|
return pfpFilename; |
||||
|
} |
||||
|
|
||||
|
public void setPfpFilename(String pfpFilename) { |
||||
|
this.pfpFilename = pfpFilename; |
||||
|
} |
||||
|
|
||||
|
public String getAgentProvider() { |
||||
|
return agentProvider; |
||||
|
} |
||||
|
|
||||
|
public void setAgentProvider(String agentProvider) { |
||||
|
this.agentProvider = agentProvider; |
||||
|
} |
||||
|
|
||||
|
public String getAgentModel() { |
||||
|
return agentModel; |
||||
|
} |
||||
|
|
||||
|
public void setAgentModel(String agentModel) { |
||||
|
this.agentModel = agentModel; |
||||
|
} |
||||
|
|
||||
|
public String getQueryRefusalResponse() { |
||||
|
return queryRefusalResponse; |
||||
|
} |
||||
|
|
||||
|
public void setQueryRefusalResponse(String queryRefusalResponse) { |
||||
|
this.queryRefusalResponse = queryRefusalResponse; |
||||
|
} |
||||
|
|
||||
|
public String getVectorSearchMode() { |
||||
|
return vectorSearchMode; |
||||
|
} |
||||
|
|
||||
|
public void setVectorSearchMode(String vectorSearchMode) { |
||||
|
this.vectorSearchMode = vectorSearchMode; |
||||
|
} |
||||
|
|
||||
|
public List<Thread> getThreads() { |
||||
|
return threads; |
||||
|
} |
||||
|
|
||||
|
public void setThreads(List<Thread> threads) { |
||||
|
this.threads = threads; |
||||
|
} |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package io.sc.platform.ai.provider.anythingllm.support.workspace.workspaces.response; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class WorkspacesResponse { |
||||
|
private List<Workspace> workspaces; |
||||
|
|
||||
|
public List<Workspace> getWorkspaces() { |
||||
|
return workspaces; |
||||
|
} |
||||
|
|
||||
|
public void setWorkspaces(List<Workspace> workspaces) { |
||||
|
this.workspaces = workspaces; |
||||
|
} |
||||
|
} |
@ -1,8 +1,8 @@ |
|||||
package io.sc.platform.ai.ollama.controller; |
package io.sc.platform.ai.provider.ollama.controller; |
||||
|
|
||||
import io.sc.platform.ai.ollama.MessageWrapper; |
import io.sc.platform.ai.api.MessageWrapper; |
||||
import io.sc.platform.ai.ollama.service.OllamaService; |
import io.sc.platform.ai.provider.ollama.service.OllamaService; |
||||
import io.sc.platform.ai.ollama.service.support.tags.TagsResponse; |
import io.sc.platform.ai.provider.ollama.service.support.tags.TagsResponse; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.*; |
import org.springframework.web.bind.annotation.*; |
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
@ -0,0 +1,14 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service; |
||||
|
|
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import io.sc.platform.ai.api.service.ApiService; |
||||
|
import io.sc.platform.ai.provider.ollama.service.support.tags.TagsResponse; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface OllamaService { |
||||
|
public ResponseBodyEmitter chat(MessageWrapper wrapper); |
||||
|
public TagsResponse tags(); |
||||
|
public List<String> modelNames(); |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.impl; |
||||
|
|
||||
|
import io.sc.platform.ai.api.service.ApiConfigurationService; |
||||
|
import io.sc.platform.ai.api.MessageWrapper; |
||||
|
import io.sc.platform.ai.provider.ollama.service.OllamaService; |
||||
|
import io.sc.platform.ai.provider.ollama.service.support.chat.ChatApi; |
||||
|
import io.sc.platform.ai.provider.ollama.service.support.tags.Model; |
||||
|
import io.sc.platform.ai.provider.ollama.service.support.tags.TagsApi; |
||||
|
import io.sc.platform.ai.provider.ollama.service.support.tags.TagsResponse; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.StringUtil; |
||||
|
import io.sc.platform.util.support.NumberStringComparator; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Service("io.sc.platform.ai.provider.ollama.service.impl.OllamaServiceImpl") |
||||
|
public class OllamaServiceImpl implements OllamaService { |
||||
|
@Autowired private ApiConfigurationService apiConfigurationService; |
||||
|
|
||||
|
@Override |
||||
|
public ResponseBodyEmitter chat(MessageWrapper wrapper) { |
||||
|
if(!StringUtil.hasText(wrapper.getModel())){ |
||||
|
wrapper.setModel(apiConfigurationService.getOllamaDefaultModelName()); |
||||
|
} |
||||
|
ChatApi api =new ChatApi(apiConfigurationService.getOllamaApiBaseUrl()); |
||||
|
return api.execute(wrapper); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public TagsResponse tags() { |
||||
|
TagsApi api =new TagsApi(apiConfigurationService.getOllamaApiBaseUrl()); |
||||
|
return api.execute(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<String> modelNames() { |
||||
|
TagsApi api =new TagsApi(apiConfigurationService.getOllamaApiBaseUrl()); |
||||
|
TagsResponse response =api.execute(); |
||||
|
if(response==null){ |
||||
|
return Collections.emptyList(); |
||||
|
} |
||||
|
List<Model> models =response.getModels(); |
||||
|
if(!CollectionUtil.hasElements(models)){ |
||||
|
return Collections.emptyList(); |
||||
|
} |
||||
|
List<String> result =new ArrayList<>(); |
||||
|
for(Model model : models){ |
||||
|
result.add(model.getModel()); |
||||
|
} |
||||
|
Collections.sort(result, new NumberStringComparator()); |
||||
|
return result; |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.blobs; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class BlobsApi extends AiApi { |
||||
|
public BlobsApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/blobs/"),"HEAD"); |
||||
|
} |
||||
|
} |
@ -1,5 +1,6 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.chat; |
package io.sc.platform.ai.provider.ollama.service.support.chat; |
||||
|
|
||||
|
import io.sc.platform.ai.provider.ollama.service.support.chat.vo.ChatResponse; |
||||
import io.sc.platform.util.ObjectMapperUtil; |
import io.sc.platform.util.ObjectMapperUtil; |
||||
import okhttp3.Call; |
import okhttp3.Call; |
||||
import okhttp3.Callback; |
import okhttp3.Callback; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.chat; |
package io.sc.platform.ai.provider.ollama.service.support.chat.vo; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
@ -1,4 +1,4 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.chat; |
package io.sc.platform.ai.provider.ollama.service.support.chat.vo; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
import com.fasterxml.jackson.annotation.JsonProperty; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.chat; |
package io.sc.platform.ai.provider.ollama.service.support.chat.vo; |
||||
|
|
||||
import java.util.ArrayList; |
import java.util.ArrayList; |
||||
import java.util.List; |
import java.util.List; |
@ -0,0 +1,4 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.chat.vo; |
||||
|
|
||||
|
public class Tool { |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.copy; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class CopyApi extends AiApi { |
||||
|
public CopyApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/copy"),"POST"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.create; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class CreateApi extends AiApi { |
||||
|
public CreateApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/create"),"POST"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.delete; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class DeleteApi extends AiApi { |
||||
|
public DeleteApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/delete"),"DELETE"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.embed; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class EmbedApi extends AiApi { |
||||
|
public EmbedApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/embed"),"POST"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.embeddings; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class EmbeddingsApi extends AiApi { |
||||
|
public EmbeddingsApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/embeddings"),"POST"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.generate; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class GenerateApi extends AiApi { |
||||
|
public GenerateApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/generate"),"POST"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.ps; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class PsApi extends AiApi { |
||||
|
public PsApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/ps"),"GET"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.pull; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class PullApi extends AiApi { |
||||
|
public PullApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/pull"),"POST"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.push; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class PushApi extends AiApi { |
||||
|
public PushApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/push"),"POST"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.show; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class ShowApi extends AiApi { |
||||
|
public ShowApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/show"),"POST"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -1,4 +1,4 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.tags; |
package io.sc.platform.ai.provider.ollama.service.support.tags; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
import com.fasterxml.jackson.annotation.JsonProperty; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.tags; |
package io.sc.platform.ai.provider.ollama.service.support.tags; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
import com.fasterxml.jackson.annotation.JsonProperty; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.platform.ai.ollama.service.support.tags; |
package io.sc.platform.ai.provider.ollama.service.support.tags; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
|
@ -0,0 +1,12 @@ |
|||||
|
package io.sc.platform.ai.provider.ollama.service.support.version; |
||||
|
|
||||
|
import io.sc.platform.ai.api.AiApi; |
||||
|
import io.sc.platform.util.UrlUtil; |
||||
|
|
||||
|
public class VersionApi extends AiApi { |
||||
|
public VersionApi(String baseUrl){ |
||||
|
super(UrlUtil.concatUrl(baseUrl,"/api/version"),"GET"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
Loading…
Reference in new issue