在目前的百模大战中,AI编程助手是程序员必不可少的东西,市面上琳琅满目的产品有没有好用一点的,方便一点的呢?今天工程师令狐向大家介绍一款极易入门的国产编程AI助手 Comate!好久没有写这种教程类的博客了,今天估摸着分享整理一下,也欢迎大家在评论区分享自己日常工作学习中用到的好用、方便的工具~
概念
Comate是一款集成了百度先进AI技术的智能编程辅助工具,它能通过深度学习理解并预测你的代码意图,大大提升编程效率,降低学习门槛,特别适合对编程尚处在摸索阶段的新手朋友。对于编程小白来说,Comate的一大亮点在于它的智能化自动补全功能。不同于传统的代码提示工具,Comate能够根据你的输入习惯、项目结构以及实际需求,动态生成最符合预期的代码片段,极大地减轻了记忆大量API和语法的工作量。此外,Comate还具备强大的错误检测与修复能力。当你的代码出现逻辑错误或语法问题时,它能迅速定位问题所在,并给出相应的修改建议,让你告别“一行代码调试一整天”的痛苦经历。
官方免费在线使用:https://comate.baidu.com/?inviteCode=midsiv0w
接下来我将带着大家展示一下工作中常用的场景:
- 错误检测与修复
- API生成代码
- 生成json格式做开发测试
使用
今天带着大家使用一下这款产品,作为Java后端选手,我选择在IDEA里向大家演示几种常见的使用。
我们直接在IDEA里的插件库里安装Comate AI
启动我们的插件工具:
错误检测与修复能力
首先我们展示一下日常工作中经常用到的场景------错误检查与修复!这个环节不用说,直接看图:
我先写一段错误代码:
public class Main { public static void main(String[] args) { HashMap<String, String> map = new HashMap<>(); map.put("bug",null); try { System.out.println(map.get("bug").toLowerCase()); } catch (NullPointerException e) { e.printStackTrace(); System.out.println("Value is null"); } } }
执行代码以后报错:
API生成代码
可以用”#“号唤醒,也可以直接点击:知识
import requests def get_weather(adcode=None, type='base', cache=None, lang='zh-cn'): """ 获取天气信息 :param adcode: 城市代码(如果不提供,系统将自动选择) :param type: base=实况天气; all=预报天气 :param cache: 是否获取缓存数据 :param lang: 语言类型(zh-cn、ru-ru、en-us、ja-jp、ko-kr) :return: 返回的天气信息 """ base_url = "http://prod-cn.your-api-server.com" # 根据实际情况选择正式环境、开发环境或测试环境 endpoint = "/location/weather" params = { 'adcode': adcode, 'type': type, 'cache': cache, 'lang': lang } response = requests.get(base_url + endpoint, params=params) if response.status_code == 200: data = response.json() if data['code'] == 0: return data['data'] # 返回天气数据 else: print(f"请求成功但返回错误:{data['msg']}") else: print(f"请求失败,状态码:{response.status_code}") return None # 示例用法 weather_data = get_weather(adcode='你的城市代码', type='base') if weather_data: print(weather_data) # 打印天气数据
当然我们可以用其他的编程语言,比如Java
import okhttp3.*; import java.io.IOException; public class WeatherApiClient { private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); private static final OkHttpClient client = new OkHttpClient(); // Base URL for development environment (change as needed) private static final String BASE_URL = "http://dev-cn.your-api-server.com"; public WeatherResponse getWeatherInfo(String adcode, String type, String cache, String lang) throws IOException { // Build the request URL with query parameters HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/location/weather").newBuilder(); if (adcode != null) urlBuilder.addQueryParameter("adcode", adcode); if (type != null) urlBuilder.addQueryParameter("type", type); if (cache != null) urlBuilder.addQueryParameter("cache", cache); if (lang != null) urlBuilder.addQueryParameter("lang", lang); HttpUrl url = urlBuilder.build(); // Create the request Request request = new Request.Builder() .url(url) .build(); // Send the request and process the response try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } else { // Parse the response body into WeatherResponse object String responseBody = response.body().string(); // Here you would typically use a JSON library like Gson or Jackson to deserialize the JSON // For simplicity, we assume the responseBody is already in the format of WeatherResponse // In a real-world scenario, you would deserialize it into WeatherResponse object // WeatherResponse weatherResponse = new Gson().fromJson(responseBody, WeatherResponse.class); // For demonstration purposes, we'll just print the response body System.out.println("Response body: " + responseBody); // Return a dummy WeatherResponse for demonstration (in a real scenario, you would return the deserialized object) return new WeatherResponse(); // Replace with actual deserialization } } } public static void main(String[] args) { WeatherApiClient client = new WeatherApiClient(); try { WeatherResponse response = client.getWeatherInfo("123456", "all", "true", "zh-cn"); System.out.println(response); // This will print the dummy WeatherResponse object } catch (IOException e) { e.printStackTrace(); } } }import okhttp3.*; import java.io.IOException; public class WeatherApiClient { private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); private static final OkHttpClient client = new OkHttpClient(); // Base URL for development environment (change as needed) private static final String BASE_URL = "http://dev-cn.your-api-server.com"; public WeatherResponse getWeatherInfo(String adcode, String type, String cache, String lang) throws IOException { // Build the request URL with query parameters HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/location/weather").newBuilder(); if (adcode != null) urlBuilder.addQueryParameter("adcode", adcode); if (type != null) urlBuilder.addQueryParameter("type", type); if (cache != null) urlBuilder.addQueryParameter("cache", cache); if (lang != null) urlBuilder.addQueryParameter("lang", lang); HttpUrl url = urlBuilder.build(); // Create the request Request request = new Request.Builder() .url(url) .build(); // Send the request and process the response try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } else { // Parse the response body into WeatherResponse object String responseBody = response.body().string(); // Here you would typically use a JSON library like Gson or Jackson to deserialize the JSON // For simplicity, we assume the responseBody is already in the format of WeatherResponse // In a real-world scenario, you would deserialize it into WeatherResponse object // WeatherResponse weatherResponse = new Gson().fromJson(responseBody, WeatherResponse.class); // For demonstration purposes, we'll just print the response body System.out.println("Response body: " + responseBody); // Return a dummy WeatherResponse for demonstration (in a real scenario, you would return the deserialized object) return new WeatherResponse(); // Replace with actual deserialization } } } public static void main(String[] args) { WeatherApiClient client = new WeatherApiClient(); try { WeatherResponse response = client.getWeatherInfo("123456", "all", "true", "zh-cn"); System.out.println(response); // This will print the dummy WeatherResponse object } catch (IOException e) { e.printStackTrace(); } } }
我们可以指定某段业务代码,然后通过AI去指定生成它的调用接口:
import java.util.HashMap; import java.util.Map; public class MapUtils { /** * Tries to retrieve and convert to lowercase a value from a given map. * If the value is null or the key is not present, it returns null. * * @param map the map to search in * @param key the key to search for * @return the lowercase value, or null if the value is null or the key is missing */ public static String getLowerCaseValue(Map<String, String> map, String key) { String value = map.get(key); if (value == null) { return null; } return value.toLowerCase(); } // This method simulates the original main function logic public static void processMapAndPrint(Map<String, String> inputMap, String keyToSearch) { try { String lowerCaseValue = getLowerCaseValue(inputMap, keyToSearch); if (lowerCaseValue != null) { System.out.println(lowerCaseValue); } else { System.out.println("Value is null or key is missing"); } } catch (NullPointerException e) { // In the new design, this should never happen as getLowerCaseValue handles nulls e.printStackTrace(); System.out.println("Unexpected NullPointerException"); } } public static void main(String[] args) { HashMap<String, String> map = new HashMap<>(); map.put("bug", null); processMapAndPrint(map, "bug"); } }
生成json格式做开发测试
这个场景也是比较常用的,对吧啊?特别是做开发测试的时候,非常的方便。
{ "请求": { "URL": "/api/processMapAndPrint", "HTTP方法": "POST", "请求头": { "Content-Type": "application/json" }, "请求体": { "inputMap": { "key1": "Value1", "key2": "VALUE2", "key3": "vAlue3" }, "keyToSearch": "key2" } }, "响应": { "状态码": 200, "响应头": { "Content-Type": "text/plain" }, "响应体": "value2" } }