Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit df60b03

Browse files
authored
remove code for proxy API (#12)
- Refactor: Removed support for `CHATGPT_ACCESS_TOKEN` and simplified the code by removing `ChatGPTUnofficialProxyAPI`. The action now requires an OpenAI API key to function. Updated instructions are provided in the README.md file.
1 parent 2497e1a commit df60b03

File tree

3 files changed

+64
-147
lines changed

3 files changed

+64
-147
lines changed

README.md

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ good for the most part.
1818
- uses: fluxninja/chatgpt-pr-reviewer@main
1919
env:
2020
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21-
CHATGPT_ACCESS_TOKEN: ${{ secrets.CHATGPT_ACCESS_TOKEN }}
2221
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
2322
with:
2423
debug: false
@@ -49,7 +48,6 @@ jobs:
4948
- uses: fluxninja/chatgpt-pr-reviewer@main
5049
env:
5150
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52-
CHATGPT_ACCESS_TOKEN: ${{ secrets.CHATGPT_ACCESS_TOKEN }}
5351
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
5452
with:
5553
debug: false
@@ -70,21 +68,11 @@ See also: [./action.yml](./action.yml)
7068
7169
#### Environment variables
7270
73-
- `GITHUB_TOKEN`
74-
- `CHATGPT_ACCESS_TOKEN`: ChatGPT access token, see also:
75-
https://github.com/acheong08/ChatGPT.
76-
77-
The access token can be easily obtained from
78-
https://chat.openai.com/api/auth/session after logging into ChatGPT.
79-
80-
- `OPENAI_API_KEY`: use this to authenticate with OpenAI API, official ChatGPT's
81-
behavior using `gpt-3.5-turbo`, see also:
82-
https://github.com/transitive-bullshit/chatgpt-api
83-
84-
Note that `CHATGPT_ACCESS_TOKEN` and `OPENAI_API_KEY` are not both required.
85-
Inside this action, unofficial ChatGPT is preferred if `CHATGPT_ACCESS_TOKEN`
86-
exists. Note that the `CHATGPT_ACCESS_TOKEN` can expire frequently, so
87-
`OPENAI_API_KEY` should be more convenient if its cost is affordable to you.
71+
- `GITHUB_TOKEN`: This should already be available to the GitHub Action
72+
environment. This is used to add comments to the pull request.
73+
- `OPENAI_API_KEY`: use this to authenticate with OpenAI API. You can get one
74+
[here](https://platform.openai.com/account/api-keys). Please add this key to
75+
your GitHub Action secrets.
8876

8977
#### Inputs
9078

@@ -160,23 +148,6 @@ jobs:
160148
See also:
161149
https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
162150

163-
### Choose the ChatGPT API implementation
164-
165-
The javascript's [chatgpt][2] package provides two implementations of the
166-
ChatGPT API:
167-
168-
- `ChatGPTAPI`: official ChatGPT using the OpenAI's `gpt-3.5-turbo`.
169-
- not free
170-
- requires `OPENAI_API_KEY`
171-
- `ChatGPTUnofficialProxyAPI`: unofficial ChatGPT models, rely on third-party
172-
server and is rate limited.
173-
- free
174-
- requires `CHATGPT_ACCESS_TOKEN`
175-
- the proxy server is configurable using `chatgpt_reverse_proxy`
176-
177-
If both environment variables `OPENAI_API_KEY` and `CHATGPT_ACCESS_TOKEN`
178-
exists, we prefer the `ChatGPTUnofficialProxyAPI` implementation.
179-
180151
### Inspect the messages between ChatGPT server
181152

182153
Set `debug: true` in the workflow file to enable debug mode, which will show the

dist/index.js

Lines changed: 16 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bot.ts

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,93 @@
1-
import './fetch-polyfill.js'
2-
import {Options} from './options.js'
3-
import * as core from '@actions/core'
4-
import {
5-
ChatGPTAPI,
6-
ChatGPTUnofficialProxyAPI,
7-
ChatMessage,
8-
SendMessageBrowserOptions,
9-
SendMessageOptions
10-
} from 'chatgpt'
1+
import "./fetch-polyfill.js";
2+
import { Options } from "./options.js";
3+
import * as core from "@actions/core";
4+
import { ChatGPTAPI, ChatMessage, SendMessageOptions } from "chatgpt";
115

126
// define type to save parentMessageId and conversationId
137
export type Ids = {
14-
parentMessageId?: string
15-
conversationId?: string
16-
}
8+
parentMessageId?: string;
9+
conversationId?: string;
10+
};
1711

1812
export class Bot {
19-
private bot: ChatGPTUnofficialProxyAPI | null = null // free
20-
private turbo: ChatGPTAPI | null = null // not free
13+
private turbo: ChatGPTAPI | null = null; // not free
2114

22-
private options: Options
15+
private options: Options;
2316

2417
constructor(options: Options) {
25-
this.options = options
26-
if (process.env.CHATGPT_ACCESS_TOKEN) {
27-
this.bot = new ChatGPTUnofficialProxyAPI({
28-
accessToken: process.env.CHATGPT_ACCESS_TOKEN,
29-
apiReverseProxyUrl: options.chatgpt_reverse_proxy,
30-
debug: options.debug
31-
})
32-
} else if (process.env.OPENAI_API_KEY) {
18+
this.options = options;
19+
if (process.env.OPENAI_API_KEY) {
3320
this.turbo = new ChatGPTAPI({
3421
systemMessage: options.system_message,
3522
apiKey: process.env.OPENAI_API_KEY,
3623
debug: options.debug,
3724
completionParams: {
38-
temperature: options.temperature
39-
}
25+
temperature: options.temperature,
26+
},
4027
// assistantLabel: " ",
4128
// userLabel: " ",
42-
})
29+
});
4330
} else {
4431
const err =
45-
"Unable to initialize the chatgpt API, both 'CHATGPT_ACCESS_TOKEN' " +
46-
"and 'OPENAI_API_KEY' environment variable are not available"
47-
throw new Error(err)
32+
"Unable to initialize the chatgpt API, both 'OPENAI_API_KEY' environment variable are not available";
33+
throw new Error(err);
4834
}
4935
}
5036

5137
chat = async (message: string, ids: Ids): Promise<[string, Ids]> => {
52-
let new_ids: Ids = {}
53-
let response = ''
38+
let new_ids: Ids = {};
39+
let response = "";
5440
try {
55-
;[response, new_ids] = await this.chat_(message, ids)
41+
[response, new_ids] = await this.chat_(message, ids);
5642
} catch (e: any) {
57-
core.warning(`Failed to chat: ${e}, backtrace: ${e.stack}`)
43+
core.warning(`Failed to chat: ${e}, backtrace: ${e.stack}`);
5844
} finally {
59-
return [response, new_ids]
45+
return [response, new_ids];
6046
}
61-
}
47+
};
6248

6349
private chat_ = async (message: string, ids: Ids): Promise<[string, Ids]> => {
6450
if (!message) {
65-
return ['', {}]
51+
return ["", {}];
6652
}
6753
if (this.options.debug) {
68-
core.info(`sending to chatgpt: ${message}`)
54+
core.info(`sending to chatgpt: ${message}`);
6955
}
7056

71-
let response: ChatMessage | null = null
72-
if (this.bot) {
73-
let opts: SendMessageBrowserOptions = {}
74-
if (ids.parentMessageId && ids.conversationId) {
75-
opts.parentMessageId = ids.parentMessageId
76-
opts.conversationId = ids.conversationId
77-
}
78-
core.info('opts: ' + JSON.stringify(opts))
79-
response = await this.bot.sendMessage(message, opts)
80-
try {
81-
core.info(`response: ${JSON.stringify(response)}`)
82-
} catch (e: any) {
83-
core.info(
84-
`response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}`
85-
)
86-
}
87-
} else if (this.turbo) {
88-
let opts: SendMessageOptions = {}
57+
let response: ChatMessage | null = null;
58+
if (this.turbo) {
59+
let opts: SendMessageOptions = {};
8960
if (ids.parentMessageId) {
90-
opts.parentMessageId = ids.parentMessageId
61+
opts.parentMessageId = ids.parentMessageId;
9162
}
92-
response = await this.turbo.sendMessage(message, opts)
63+
response = await this.turbo.sendMessage(message, opts);
9364
try {
94-
core.info(`response: ${JSON.stringify(response)}`)
65+
core.info(`response: ${JSON.stringify(response)}`);
9566
} catch (e: any) {
9667
core.info(
97-
`response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}`
98-
)
68+
`response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}`,
69+
);
9970
}
10071
} else {
101-
core.setFailed('The chatgpt API is not initialized')
72+
core.setFailed("The chatgpt API is not initialized");
10273
}
103-
let response_text = ''
74+
let response_text = "";
10475
if (response) {
105-
response_text = response.text
76+
response_text = response.text;
10677
} else {
107-
core.warning('chatgpt response is null')
78+
core.warning("chatgpt response is null");
10879
}
10980
// remove the prefix "with " in the response
110-
if (response_text.startsWith('with ')) {
111-
response_text = response_text.substring(5)
81+
if (response_text.startsWith("with ")) {
82+
response_text = response_text.substring(5);
11283
}
11384
if (this.options.debug) {
114-
core.info(`chatgpt responses: ${response_text}`)
85+
core.info(`chatgpt responses: ${response_text}`);
11586
}
11687
const new_ids: Ids = {
11788
parentMessageId: response?.id,
118-
conversationId: response?.conversationId
119-
}
120-
return [response_text, new_ids]
121-
}
89+
conversationId: response?.conversationId,
90+
};
91+
return [response_text, new_ids];
92+
};
12293
}

0 commit comments

Comments
 (0)