当前位置:   article > 正文

Claude2 Api接入方案(现在官方提供api了)

claude2 api

一,实现目标

接入钉钉机器人支持群聊和私聊

网上看了很多方案,因为Claude的api申请难度非常大,大部分都是说使用Slack,但是Slack只能免费使用一个月。作为一个完美主义怎么可能允许这样的事情发生。何如处理,抓网页。

只能美,英,魔法这些都是入门小菜,这里不再赘述。自行解决。

 二,关键接口

1,获取organizationUuid

String url = "https://claude.ai/api/organizations";

2,新增会话

String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations", organizationUuid);

返回 conversationUuid

3,获取单个会话

String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations/%s", organizationUuid, conversationUuid);

4,获取会话列表

String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations", organizationUuid);

5,删除会话

String url = String.format("https://claude.ai/api/organizations/%s/chat_conversations/%s", organizationUuid, conversationUuid);

7,发送消息

String url = "https://claude.ai/api/append_message";

三,关键代码

一下代码都是他自己生成的,活学活用。

1,请求代理

  1. public class RestTemplateFactory {
  2. public static RestTemplate getRestTemplate() {
  3. return new RestTemplate();
  4. }
  5. public static RestTemplate getProxyRestTemplate(Integer type) {
  6. RestTemplateConf restTemplateConf = new RestTemplateConf();
  7. try {
  8. if (type == 1) {
  9. return restTemplateConf.getRestTemplateHongKong();
  10. } else if (type == 2) {
  11. return restTemplateConf.getRestTemplateWashington();
  12. } else if (type == 3) {
  13. return restTemplateConf.getProxyRestLocal();
  14. } else if (type == 4) {
  15. return restTemplateConf.getRestTemplate();
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. return new RestTemplate();
  21. }
  22. }
  1. public class RestTemplateConf {
  2. private String hostname;
  3. private Integer port;
  4. private String username;
  5. private String password;
  6. public RestTemplateConf() {
  7. }
  8. private RestTemplateConf(String hostname, Integer port, String username, String password) {
  9. this.hostname = hostname;
  10. this.port = port;
  11. this.username = username;
  12. this.password = password;
  13. }
  14. public RestTemplate getRestTemplateHongKong() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
  15. RestTemplateConf restTemplateConf = new RestTemplateConf("xxxx", xxxx, "xxxx", "xxxx");
  16. return restTemplateConf.getRestTemplateProxy();
  17. }
  18. public RestTemplate getRestTemplateWashington() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
  19. RestTemplateConf restTemplateConf = new RestTemplateConf("xxxx", xxxx, "xxxx", "xxxx");
  20. return restTemplateConf.getRestTemplateProxy();
  21. }
  22. public RestTemplate getProxyRestLocal() {
  23. HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
  24. // 配置代理
  25. HttpHost proxy = new HttpHost("127.0.0.1", 1087);
  26. httpClientBuilder.setProxy(proxy);
  27. // 配置其他HttpClient的相关参数
  28. RequestConfig requestConfig = RequestConfig.custom()
  29. // 可根据需求进行配置
  30. .setConnectTimeout(120000)
  31. .setSocketTimeout(120000)
  32. .build();
  33. httpClientBuilder.setDefaultRequestConfig(requestConfig);
  34. // 将配置好的HttpClient应用到RestTemplate
  35. return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()));
  36. }
  37. public RestTemplate getRestTemplate() {
  38. HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
  39. factory.setConnectTimeout(120000);
  40. factory.setReadTimeout(120000);
  41. return new RestTemplate(factory);
  42. }
  43. private RestTemplate getRestTemplateProxy() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
  44. RestTemplate restTemplate = new RestTemplate();
  45. restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
  46. HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
  47. // 配置其他HttpClient的相关参数
  48. RequestConfig requestConfig = RequestConfig.custom()
  49. // 可根据需求进行配置
  50. .setConnectTimeout(120000)
  51. .setSocketTimeout(120000)
  52. .build();
  53. httpClientBuilder.setDefaultRequestConfig(requestConfig);
  54. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  55. public boolean isTrusted(X509Certificate[] arg0, String arg1) {
  56. return true;
  57. }
  58. }).build();
  59. httpClientBuilder.setSSLContext(sslContext);
  60. //设置代理
  61. this.setProxy(restTemplate);
  62. //设置代理密码
  63. this.setCredentialsProvider(httpClientBuilder);
  64. HttpClient httpClient = httpClientBuilder.build();
  65. // httpClient连接配置
  66. HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
  67. restTemplate.setRequestFactory(clientHttpRequestFactory);
  68. return restTemplate;
  69. }
  70. private void setProxy(RestTemplate restTemplate) {
  71. SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  72. Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
  73. requestFactory.setProxy(proxy);
  74. restTemplate.setRequestFactory(requestFactory);
  75. }
  76. private void setCredentialsProvider(HttpClientBuilder httpClientBuilder) {
  77. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  78. credentialsProvider.setCredentials(
  79. new AuthScope(hostname, port),
  80. new UsernamePasswordCredentials(username, password)
  81. );
  82. HttpHost proxy = new HttpHost(hostname, port);
  83. httpClientBuilder.setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).disableCookieManagement();
  84. }

2,发送消息

  1. /**
  2. * 获取问题答案
  3. */
  4. public static String getAnswer(List<Message> messages, String organizationUuid, String conversationUuid) {
  5. String organizations = null;
  6. String conversations = null;
  7. if (organizationUuid == null || conversationUuid == null) {
  8. organizations = getOrganizations();
  9. organizationUuid = JSONUtil.parseArray(organizations).getJSONObject(0).getStr("uuid");
  10. conversations = addChatConversation(organizationUuid, "问答");
  11. conversationUuid = JSONUtil.parseObj(conversations).getStr("uuid");
  12. }
  13. List<String> attachments = new ArrayList<>();
  14. if (messages.size() != 1) {
  15. for (int i = 0; i < messages.size() - 1; i++) {
  16. Message message = messages.get(i);
  17. if ("user".equals(message.getRole())) {
  18. try {
  19. sendMessage(organizationUuid, conversationUuid, messages.get(i).getContent(), attachments);
  20. } catch (UnsupportedEncodingException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. }
  26. Message message = messages.get(messages.size() - 1);
  27. List<String> responses = null;
  28. try {
  29. responses = sendMessage(organizationUuid, conversationUuid, message.getContent(), attachments);
  30. } catch (UnsupportedEncodingException e) {
  31. e.printStackTrace();
  32. }
  33. //deleteChatConversation(organizationUuid, conversationUuid);
  34. return String.join("", responses).trim();

四,机器人配置

1,方案一,不能用挺久了,这种支持外部群

2,方案二 

企业内部应用 

开发者后台统一登录 - 钉钉统一身份认证

配置好即可

五,源码

项目地址,欢迎交流

java 版和 python 版都有

https://gitee.com/g7go/chat-ai

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/365285
推荐阅读
相关标签
  

闽ICP备14008679号