OpenAi使用心得(注册+聊天+CodeX+图片+对接api)

OpenAi使用心得(注册+聊天+CodeX+图片)


博主自研AI永久公益: 慧言AI


一、聊天功能:

image-20230213171547070

二、 CodeX(白话文编码)

这里直接录了个ai写网页的视频 成品展示:成品

三、图片生成功能

输入关键词即可直接生成相关图片,包含输入元素
image-20230213171922082

四、API对接

成品展示

这边复制一个demo进行api对接,导入接口测试工具即可
其中API_KEY应替换为你自己的

curl --location --request POST 'https://api.openai.com/v1/completions' \
--header 'accept: application/json' \
--header 'Authorization: Bearer 你的API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
  "model": "text-davinci-003",
  "prompt": "\nHuman: 你可以帮我查一下天气吗?\nAI: 当然可以!请问您想查询哪个城市的天气?\nHuman: 北京",
  "temperature": 0.49,
  "max_tokens": 4000,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0.6,
  "stop": [" Human:", " AI:"]
}'
在Java中调用OpenAI API通常需要通过HTTP请求库,如Apache HttpClient、OkHttp或者Spring的RestTemplate来进行。以下是一个简单的示例,展示如何使用HttpClient发送GET请求并处理JSON响应: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class OpenAIAPICaller { private static final String OPENAI_API_KEY = "your_openai_api_key"; private static final String OPENAI_ENDPOINT = "https://api.openai.com/v1/engines/davinci-codex/completions"; public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet(OPENAI_ENDPOINT); httpGet.setHeader("Authorization", "Bearer " + OPENAI_API_KEY); CloseableHttpResponse response = httpClient.execute(httpGet); try { HttpEntity entity = response.getEntity(); if (entity != null) { String responseBody = EntityUtils.toString(entity, "UTF-8"); // 这里你可以解析返回的JSON数据,例如: // JSONObject json = new JSONObject(responseBody); // System.out.println(json.getString("text")); } else { System.out.println("No data received from the API."); } } finally { response.close(); } } finally { httpClient.close(); } } } ``` 在这个例子中,你需要替换`your_openai_api_key`为你实际的OpenAI API密钥,并确保你已经添加了必要的依赖于处理HTTP请求和JSON。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值