<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nginx归档 - Liao&#039;s blog</title>
	<atom:link href="https://www.laobaiblog.top/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.laobaiblog.top/tag/nginx/</link>
	<description>路漫漫其修远兮，吾将上下而求索</description>
	<lastBuildDate>Mon, 15 Jan 2024 07:50:02 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.laobaiblog.top/wp-content/uploads/2022/01/cropped-tyuu-32x32.png</url>
	<title>nginx归档 - Liao&#039;s blog</title>
	<link>https://www.laobaiblog.top/tag/nginx/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>使用Nginx转发代理ChatGpt接口，Java调用实现多轮对话</title>
		<link>https://www.laobaiblog.top/2024/01/15/%e4%bd%bf%e7%94%a8nginx%e8%bd%ac%e5%8f%91%e4%bb%a3%e7%90%86chatgpt%e6%8e%a5%e5%8f%a3%ef%bc%8cjava%e8%b0%83%e7%94%a8%e5%ae%9e%e7%8e%b0%e5%a4%9a%e8%bd%ae%e5%af%b9%e8%af%9d/</link>
		
		<dc:creator><![CDATA[大白]]></dc:creator>
		<pubDate>Mon, 15 Jan 2024 07:50:02 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[ChatGPT]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[域名]]></category>
		<guid isPermaLink="false">https://www.laobaiblog.top/?p=462</guid>

					<description><![CDATA[<p>一、理论概述 ChatGpt接口目前只能在国外互联网环境中使用，国内想要使用相关服务的话可以寻求代理 &#8230;</p>
<p><a href="https://www.laobaiblog.top/2024/01/15/%e4%bd%bf%e7%94%a8nginx%e8%bd%ac%e5%8f%91%e4%bb%a3%e7%90%86chatgpt%e6%8e%a5%e5%8f%a3%ef%bc%8cjava%e8%b0%83%e7%94%a8%e5%ae%9e%e7%8e%b0%e5%a4%9a%e8%bd%ae%e5%af%b9%e8%af%9d/">使用Nginx转发代理ChatGpt接口，Java调用实现多轮对话</a>最先出现在<a href="https://www.laobaiblog.top">Liao&#039;s blog</a>。</p>
]]></description>
										<content:encoded><![CDATA[<h3>一、理论概述</h3>
<p>ChatGpt接口目前只能在国外互联网环境中使用，国内想要使用相关服务的话可以寻求代理，在这里使用的是一台国外服务器安装Nginx服务进行相关代理。</p>
<h3>二、环境要求</h3>
<ul>
<li>一台拥有公网IP的国外服务器</li>
<li>一个域名及免费ssl证书（可在腾讯云免费申请ssl证书）</li>
<li>一个ChatGpt账号及相关的<a class="wp-editor-md-post-content-link" href="https://platform.openai.com/api-keys">APIkey</a></li>
</ul>
<p>以上就是所有需要用到的环境，<strong><em>请注意，如要使用接口还需要<a class="wp-editor-md-post-content-link" href="https://platform.openai.com/account/billing/overview">充值API</a>额度</em></strong>。除此之外还需要将你的域名解析到公网IP上。完成准备工作后进行以下操作。</p>
<h3>三、Nginx转发</h3>
<ol>
<li>在服务器中安装nginx，这里使用的是docker服务，根据以往博文中可自行安装Docker服务。</li>
</ol>
<pre><code class="language-shell line-numbers">#服务器中新建一个nginx目录，下级目录新建conf.d、logs、ssl三个目录
mkdir -p nginx/conf.d
mkdir -p nginx/logs
mkdir -p nginx/ssl

#在nginx目录中新建start.sh脚本
vim start.sh

docker stop nginx
docker rm nginx
docker run  -p 80:80  -p 443:443 --name nginx --net=host  --restart=always  --privileged=true \
    -v `pwd`/conf.d/:/etc/nginx/conf.d \
    -v `pwd`/logs/:/var/log/nginx  \
    -v `pwd`/ssl/:/var/ssl \
    -e TZ="Asia/Shanghai" \
    nginx:latest

#输入 :wq 保存后授权并执行即可
chmod a+x start.sh
./start.sh
</code></pre>
<ol start="2">
<li>将域名ssl证书放置在ssl目录下后，编辑nginx转发代理，执行<code>start.sh</code>脚本重启nginx服务。</li>
</ol>
<pre><code class="language-shell line-numbers">vim conf.d/gpt.conf

#填入域名信息
server {
        listen       443 ssl;
        server_name  域名;
        ssl_certificate /var/ssl/域名.crt;     #配置证书位置
        ssl_certificate_key /var/ssl/域名.key; #配置秘钥位置】

        ssl_session_timeout 5m;
        ssl_protocols SSLv2 SSLv3 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers on;

        underscores_in_headers on;
        charset utf-8;

        location /v1/ {
             proxy_ssl_server_name on;
             proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
             proxy_pass https://api.openai.com;
        }
}
</code></pre>
<p>以上便完成了针对chatgpt接口地址 <strong><em><code>api.openai.com</code></em></strong> 的转发工作，转发后的地址既是你的域名。</p>
<h3>四、Java实例调用实现多轮对话</h3>
<ol>
<li>新建Maven项目ChatGPTClient，在<code>pom.xml</code>中添加后续用到的依赖，重构后拉取。</li>
</ol>
<pre data-language=XML><code class="language-markup line-numbers">&lt;dependency&gt;
            &lt;groupId&gt;com.squareup.okhttp3&lt;/groupId&gt;
            &lt;artifactId&gt;okhttp&lt;/artifactId&gt;
            &lt;version&gt;4.9.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
            &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
            &lt;artifactId&gt;gson&lt;/artifactId&gt;
            &lt;version&gt;2.8.9&lt;/version&gt; &lt;!-- 使用最新的可用版本 --&gt;
&lt;/dependency&gt;
</code></pre>
<ol start="2">
<li>java代码</li>
</ol>
<pre><code class="language-shell line-numbers">import okhttp3.*;
import com.google.gson.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ChatGPTClient {
    private final String apiKey;
    private final OkHttpClient client;
    private final List&lt;Message&gt; messages;
    private final Gson gson;
    private int tokenCount = 0;
    private int inputTokenCount = 0;
    private int outputTokenCount = 0;

    public ChatGPTClient(String apiKey) {
        this.apiKey = apiKey;
        this.client = new OkHttpClient();
        this.messages = new ArrayList&lt;&gt;();
        this.gson = new Gson();
    }

    public String sendMessage(String message) throws IOException {
        updateTokenCount(message, true);

        messages.add(new Message("system", "user", message));
//model模型选择可以有gpt-3.5-turbo、gpt-4、gpt-4-turbo价格以此类推
        RequestBody body = RequestBody.create(
                MediaType.get("application/json; charset=utf-8"),
                "{\"model\": \"gpt-3.5-turbo\", \"messages\": " + gson.toJson(messages) + "}"
        );

        Request request = new Request.Builder()
                .url("https://你的域名地址/v1/chat/completions")
                .addHeader("Authorization", "Bearer " + this.apiKey)
                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            JsonObject respJson = JsonParser.parseString(response.body().string()).getAsJsonObject();
            JsonArray choices = respJson.getAsJsonArray("choices");
            if (choices != null &amp;&amp; choices.size() &gt; 0) {
                JsonObject firstChoice = choices.get(0).getAsJsonObject();
                String aiResponse = firstChoice.getAsJsonObject("message").get("content").getAsString();

                updateTokenCount(aiResponse, false);

                messages.add(new Message("system", "assistant", aiResponse));

                System.out.println("输入的 Token 数: " + inputTokenCount);
                System.out.println("输出的 Token 数: " + outputTokenCount);
                System.out.println("总使用的 Token 数: " + tokenCount);

                return aiResponse;
            }
        }
        return "无法获取响应。";
    }

    private void updateTokenCount(String text, boolean isInput) {
        int count = 0;
        for (char c : text.toCharArray()) {
            count += (String.valueOf(c).matches("[\\u0000-\\u00ff]") ? 1 : 4);
        }
        int tokens = (int) Math.ceil(count / 4.0);
        tokenCount += tokens;
        if (isInput) {
            inputTokenCount += tokens;
        } else {
            outputTokenCount += tokens;
        }
    }

    private static class Message {
        String role;
        String content;

        public Message(String type, String role, String content) {
            this.role = role;
            this.content = content;
        }
    }

    public static void main(String[] args) throws IOException {
        ChatGPTClient client = new ChatGPTClient("YOUR-API-KEY");

        Scanner scanner = new Scanner(System.in, "UTF-8");
        String input;
        while (true) {
            System.out.print("输入消息（输入 'bye' 结束对话）: ");
            input = scanner.nextLine();
            if ("bye".equalsIgnoreCase(input)) {
                break;
            }
            String response = client.sendMessage(input);
            System.out.println("ChatGPT 回应: " + response);
        }
    }
}

</code></pre>
<p>在以上代码中，增加了每次对话后所使用的token值，以便查询核对接口使用费。可在官网查询相关定价。</p>
<p><a class="wp-editor-md-post-content-link" href="https://www.laobaiblog.top/wp-content/uploads/2024/01/wp_editor_md_7ca6ffab890759733ec128bda9db6a20.jpg"><img decoding="async" src="https://www.laobaiblog.top/wp-content/uploads/2024/01/wp_editor_md_7ca6ffab890759733ec128bda9db6a20.jpg" alt="" /></a></p>
<h3>五、运行结果</h3>
<p><a class="wp-editor-md-post-content-link" href="https://www.laobaiblog.top/wp-content/uploads/2024/01/wp_editor_md_86a520f2c97f583072037ca931be6455.jpg"><img decoding="async" src="https://www.laobaiblog.top/wp-content/uploads/2024/01/wp_editor_md_86a520f2c97f583072037ca931be6455.jpg" alt="" /></a></p>
<p><a href="https://www.laobaiblog.top/2024/01/15/%e4%bd%bf%e7%94%a8nginx%e8%bd%ac%e5%8f%91%e4%bb%a3%e7%90%86chatgpt%e6%8e%a5%e5%8f%a3%ef%bc%8cjava%e8%b0%83%e7%94%a8%e5%ae%9e%e7%8e%b0%e5%a4%9a%e8%bd%ae%e5%af%b9%e8%af%9d/">使用Nginx转发代理ChatGpt接口，Java调用实现多轮对话</a>最先出现在<a href="https://www.laobaiblog.top">Liao&#039;s blog</a>。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
