当前位置:   article > 正文

AI机器人互相聊天_ai聊天

ai聊天

这里采用的青云客的聊天机器人和图灵机器人,让他们互相聊天看看可以擦除怎样的火花

青云客聊天机器人接口调用demo:

/**青云客*/
	public static void startChat(){
		Scanner scanner = new Scanner(System.in);
		while(true){
			String msg = scanner.nextLine();
			//String msg = tulingSay;
			msg = msg.replace(" ", "");
			//System.out.println(msg);
			if (msg!=null){
				String url;
				url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+msg;
				Map<String,Object> dataMap = HttpUtils.getCommonMap(url);
				System.out.println(dataMap);
				if(dataMap!=null){
					if(dataMap.get("result").equals(0)){
						String back = (String)dataMap.get("content");
						cloudSay = back;
						System.out.println("青云说:"+back);
					}
				}
			}
			try {
				Thread.sleep(15000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

运行结果
在这里插入图片描述

图灵机器人聊天demo(注意 图灵机人的每天聊天消息只有100条):

/**图灵机器人*/
	public static void startChat2(){
		Scanner scanner = new Scanner(System.in);
		while(true){
			String msg = scanner.nextLine();//cloudSay;
			msg = msg.replace(" ", "");
			if (msg!=null){
				String url;
				url = "http://openapi.tuling123.com/openapi/api/v2";
				Map<String,Object> dataMap = HttpUtils.postCommonMap(url,msg);
				System.out.println(dataMap);
				if(dataMap!=null){
				  JSONArray data = (JSONArray)dataMap.get("results");
				  @SuppressWarnings("unchecked")
				  String back = ((Map<String,Object>)((Map<String,Object>)data.get(0)).get("values")).get("text").toString();
				  tulingSay = back;
				  System.out.println("图灵说:"+back);
				}
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

运行结果:在这里插入图片描述
HttpClient请求类:

package com.nms.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
* @author 作者SMF:
* @version 创建时间:2018年12月30日 上午10:04:17
* 类说明  HTTP请求 类
*/
public class HttpUtils {

	 
   /**
    * 发送 get请求
    */
   public static String get(String url) {
       CloseableHttpClient httpclient = HttpClients.createDefault();
       String result = null;
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/536163
推荐阅读
相关标签
  

闽ICP备14008679号