掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 3749|回复: 6

[词典讨论] GoldenDict + ChatGPT(利用 OpenAI API)[20230503]

[复制链接]

该用户从未签到

发表于 2023-5-3 13:11:27 | 显示全部楼层 |阅读模式

% Q' P5 c! D% @2 ]参考:: l" x% ^3 v- e" b
- [词典讨论] ChatGPT 用作词典 https://www.pdawiki.com/forum/fo ... hread&tid=48326
  B2 t. k/ {8 F, c- [经验交流] GoldenDict 有X词典在线版(Python 在线版) https://pdawiki.com/forum/forum.php?mod=viewthread&tid=332037 l- a. r0 I4 c* e' u( r1 i
- GoldenDict + Python 调用有道翻译[...]  https://pdawiki.com/forum/forum.php?mod=viewthread&tid=21988! @; _& y6 h3 D8 G' m) W
- [5.16]goldendict 有道释义 & google翻译 [python] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=26565: ]4 J! o4 l8 D- f$ |0 f  p
- 人人词典GoldenDict插件[附python3源码] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=31713- @" m# U) \% C$ i+ S" c' ~/ E
- ChatGPT-Next-Web https://github.com/Yidadaa/ChatGPT-Next-Web5 ^# Z9 J( s0 h* o8 b& I6 ?: ~
+ D4 k/ C" K# a

1 B! D1 s$ [5 ~# T前置条件:
# V  L' d5 i8 B: p1、OpenAI API key https://platform.openai.com/account/api-keys3 w5 z6 R6 h$ Z% U4 X" s, w( }
2、Python
4 e; Z; u! u- h1 n7 F5 ?  n2 P' l2 ?9 U% {$ g
问:
& z) O: [" ~& y$ A9 A4 x( u
1、写一个 mydict.py,调用 OpenAI API key,实现查询功能。. K, ^6 I7 f# [" Z8 k6 V
2、用 GoldenDict 调用 mydict.py。

. m4 J- W1 I: q2 V! |1 c2 O% |- H+ s. H& E+ Y& g8 D$ {
ChatGPT 回答:
/ d' R$ z" p) L9 M
这里提供一个基于 OpenAI GPT-3 API 的简单 mydict.py 脚本示例,可以通过命令行传入要查询的单词,输出 OpenAI API 返回的结果:$ q8 b$ {0 x7 W" F
3 O9 Z1 G* m: J  J6 D& j
```python
# {! R+ h) X' h0 }7 m5 E* H8 Simport openai& x. q6 R- ^% u% j/ [5 Y
import sys- p: E. D( f- z% ^, r9 q
$ ]& s( u5 s# k( M& ?- g5 q6 t
openai.api_key = "YOUR_OPENAI_API_KEY_HERE"
3 G$ |# z3 o; T1 D7 O7 |
# g7 B3 }: r  mdef define(word):* m& ?# G% Y4 T3 ^5 h0 U
    prompt = f"Define '{word}'"; \) e' ^" c! V8 w( P( V
    response = openai.Completion.create(
# i9 }% v& [5 O9 h        engine="text-davinci-002",
7 l7 f6 j3 x/ e4 p3 a        prompt=prompt,/ z3 x1 B9 [* Q# K0 e* _6 o6 T0 r5 {  ?
        max_tokens=1024,( t/ B& ?& ]* X1 z+ Z1 x
        n=1,
% ~: G+ L6 t  J$ a4 w) k8 _        stop=None,4 k4 e0 v& z+ U6 w) C
        temperature=0.5,2 v8 l8 d, F7 p4 i  ~+ E5 A: R7 f
    )
$ M( d# M, A0 W& @( Q9 R
% x, t6 z+ E0 @) i( T1 ~9 e    if len(response.choices) > 0:" ?  r* t, v7 x! [: N! [: U6 {4 s
        return response.choices[0].text
* H5 [0 E) e9 t( _- c4 P" `    else:/ s$ ?7 }) B5 s+ p) b
        return "No definition found.", ?, X& g- ?9 Z4 F( p4 u' f

% g& N8 S7 t; O( y6 |! wif __name__ == "__main__":8 K: x5 P& S9 v" J- h$ K
    word = sys.argv[1]$ l2 Y. P/ U7 k$ o
    definition = define(word)
1 Y+ K" R+ D# K0 t- |. D    print(definition)4 h6 G- a5 O" Y' b% F, L! N2 @
```) W1 G- N6 U9 L) C* X. c6 a: {
+ v+ P/ {7 E* p  X# X
将上述脚本保存为 `mydict.py` 文件。
0 }: P4 t) Z5 f) e% V$ \+ T# ~3 e2 {8 C, z: U
接下来,在 GoldenDict 中设置该脚本为自定义命令行词典。+ [" l- J/ A; m& `7 |/ L8 E& S% _

$ G* i# @; S4 \8 `0 \% n4 Y7 ]1. 打开 GoldenDict,选择 Edit > Dictionaries。
) P2 e; Y8 D1 a* J5 I& {2. 在 Programs 标签页中,点击 Add 按钮。
6 |8 V& O2 s. f, j3. 在 Dialog 标题栏中输入 “MyDict”,在 Command 栏中输入 `python /path/to/mydict.py %GDWORD%`(其中 `/path/to/mydict.py` 为脚本的绝对路径),点击 OK 按钮。
* x9 P$ ^" }7 `/ x5 O3 {4. 在主界面中,选中一段文本,然后按下 Ctrl+C+C,即可通过 mydict.py 查询该单词的定义。

5 C) U- R/ F! Q, G& d* u0 b7 s) N& J+ i# X2 n! V% a7 M
$ _$ v8 f; k/ d+ p9 S  t7 M
) j2 Y' X# d$ e9 y

评分

1

查看全部评分

本帖被以下淘专辑推荐:

  • TA的每日心情
    开心
    2019-8-21 08:44
  • 签到天数: 163 天

    [LV.7]常住居民III

    发表于 2023-5-5 10:16:19 | 显示全部楼层
    本帖最后由 mikeee 于 2023-5-5 11:26 编辑
    * m$ t1 x) z* J8 {* X' T, ]
    1 Z1 x; g& Q+ L 链接: https://pan.baidu.com/s/1HXpt0U3vb7GJDKpBNm_FOw?pwd=4vph 提取码: 4vph
    6 e! x7 s; x% t+ }- i$ f$ E- L! F* @, M; r
    这个命令行工具`chatgpt_tr`应该可以设置成在 GoldenDict 里使用。用的是令牌,即是说免费的。
    ) ]7 `, f, G1 e3 G6 i+ J) U1 ~- B( G+ p, Q2 W: D
    chatgpt_tr主要是给命令行翻译用的,但也可以自定义提示Prompt以及用于对话。详细用法参看 ``chatgpt_tr.exe --help``。" k* W6 Z1 {$ ~! Q6 l9 @3 J6 x( I' l
    % i3 b$ c+ F: Z$ A$ u- c) x" {  c
    用法
    9 g0 R, Y3 N3 a7 d4 E
    * ?/ U- _/ b. C( ?
    • 解压
    • 获取access token
      , U7 S6 i' j, c: a" V6 P7 z+ l0 f
      • 登录 chat.openai.com
      • 访问 https://chat.openai.com/api/auth/session
      • 拷 accessToken备用
      • 此操作只需一次。(access token 有效期约14-30天,但内地ip可能需双向提子因为openai和大伙墙的双重封杀,港澳ip则需找未被openai封杀的带理)
        4 F- g0 g+ W6 ?, y6 n; C1 O
    • 命令行下设置环境变量OPENAI_ACCESS_TOKEN,例如cmd下 set OPENAI_ACCESS_TOKEN=eyJhbGc... 或用包内的 chatgpt-tr.bat 或sysdm.cpl设置全局环境变量OPENAI_ACCESS_TOKEN
    • 命令行下,例如WIN cmd回车(或开Windows Terminal或cmder或conemu)
      2 F) J( D; j6 l( `/ `; ], z
      • chatgpt_tr --help # 帮助
      • chatgpr_tr # 翻译系统剪贴板内容
      • chatgpr_tr this is a test # 翻译 this is a test
        9 U4 y. j4 A( w! ^  C" [

      : a- G, f) f3 v& G- d. Z( Q1 E/ \
    4 i- H$ \5 }9 r; t- \# I) d4 s4 m
    可以利用 bat 自定义提示Prompt各种使用场景(例如`chatgpt_tr.exe -p "列出5个中文同义词"  make sense` )。该工具的python源码核心部分只有十几行。网友有兴趣的话我可以提供python源码。6 ^6 }  G, j! v9 _6 D: ^2 Z
    - g0 h; _1 n% c  [% w- }9 P
    该工具基于某个第三方反向代理,代理挂了或被关闭当然工具也就不能用了。 (实测2023-05-05可用)" Q6 g' ~$ q# p4 b% J( s1 ?' Z8 u5 s

    ' i. B" c9 @$ Y8 H典型输出:
    , S0 `: y2 I6 [3 @) \+ D$ B/ z1 ~
    # chatgpt_tr amazon code whisperer
      c! P) H  f" h3 ~$ U( R+ B! eamazon code whisperer
    $ }& k* i5 l6 L' n; R
    7 U( G5 g4 r- X4 m3 z版本1:
    8 k- s  u# @0 ^" K& T  j亚马逊代码耳语师6 X* w1 D+ k( l+ X, ?' L) _5 p
    0 \9 D3 @2 ?( h& Y
    版本2:* d/ Q+ s' p* ]# q+ R
    亚马逊代码密语者
    / O; r9 f  n! W
    / k# k, S# ?! \1 s: }/ m版本3:
    : ^' j2 n! q" W$ {& u4 X3 t5 Z亚马逊代码秘语师' R5 D; {( c, q; M$ t
    - i) s. f! S% _3 ?" c
    # chatgpt_tr.exe -p "列出5个中文同义词" make sense
    3 k" T0 Y/ J; i, Dmake sense' ~$ x7 r& z. C/ R  h

      E8 I2 A9 w( V1. 有道理
    1 K; w8 r0 @5 d7 L: N3 o+ W" J5 U2. 讲得通% d# m2 B. G& a
    3. 合理- e0 F0 G' [9 N2 `4 h2 ]7 {
    4. 明智' b* t! I- W6 j
    5. 理性
    ' u7 f! N/ N  O! Y
    - }+ e4 w$ z5 N8 L! c4 G# chatgpt_tr.exe --help
    & h+ \: B# p6 o) D
    3 N% b' U; H7 @) |# m  D8 { Usage: chatgpt_tr.exe [OPTIONS] [TEXT]...
    ' P+ E2 A/ s) p, {# X2 {) z2 Q" P1 t
    Translate with chatgpt using access token.
    ) U0 _1 M+ x# h/ F4 K4 b1 t set LOGURU_LEVEL=DEBUG or set LOGLEVEL=10 to turn on debug/verbose mode.
    + v9 |" C  b( A% k6 l$ M6 y# ~# B$ f! ^4 M3 s3 f
    ┌─ Arguments ──────────────────────────────────────────────────────────────────┐% {1 K8 c, c* t4 {
    │   text      [TEXT]...  Source text. [default: None]                          │9 _( ]) Y  a3 w: [6 T, y; H$ }
    └──────────────────────────────────────────────────────────────────────────────┘" }5 w8 G+ P- W4 r$ k' ~+ J
    ┌─ Options ────────────────────────────────────────────────────────────────────┐
    * a! R# G8 }6 n( M6 N3 n. \! x│ --version  -v,-V               Show version info and a list of supported     │
    7 r3 Z1 d; \; u& O+ X. [│                                languages and exit.                           │( F& n$ S" u- Z
    │ --clipb    -c                  Use clipboard content if set or if `text` is  │! ^- |6 F3 {6 E! Z6 f/ w# n! c# \
    │                                empty.                                        │7 C$ V4 n1 i/ P# N
    │ --to-lang  -t         TEXT     Target language when using the default        │
    ) V" b/ o% G3 s  e. G% e: k8 V" m│                                prompt.                                       │" x( W6 _% R1 l- L- h2 ~/ @
    │                                [default: 中文]                               │) a/ [3 p- O7 u: h: ^3 Z" N5 l( N
    │ --numb     -n         INTEGER  number of translation variants when using the │5 Z1 \0 X9 Z& ^$ o0 b; H
    │                                default prompt.                               │7 g! p% I3 n0 \9 h0 {7 \3 S2 \* W
    │                                [default: 3]                                  │
    3 q. K+ A6 [! a% n; X( i: n) |│ --prompt   -p         TEXT     User defined prompt. [default:                │
    % t7 r0 K5 ^# n3 ~  \│                                '将以下内容内容翻成 to_lang                   │  g, s8 ~* k% w9 u' I' ~
    │                                (中文),不要自由发挥,列出 numb (3) 个版本:  │
    - a2 Y. O3 z/ V/ `6 Q; }" z! I4 i│                                \n']                                          │8 x7 P! P2 E) Z$ l1 J, F- E) b
    │ --help                         Show this message and exit.
  • TA的每日心情
    开心
    2018-8-26 14:41
  • 签到天数: 45 天

    [LV.5]常住居民I

    发表于 2023-5-3 20:03:13 | 显示全部楼层
    有点浪费token

    该用户从未签到

    发表于 2023-5-3 17:31:29 | 显示全部楼层
    我还是觉得GoldenDict + ChatGPT这个组合算拉郎配8 O2 h* m$ c9 G# x

    ' q& x$ ?, `; B; [3 O  L: O% p别的不说,就看下图吧:- \/ @5 {, J- Z' w2 y& b3 Z8 _1 D

    + @/ H! ~1 G2 G! b' M6 n) g# p& p
    7 ]8 X- W9 {& O( Q  |& P+ A! c6 E' C, D, {9 j
    Define指令不支持中文 用中文吧,它给的解释还是错的
    * |- P" U' {7 h) S) |- [$ O  t+ ?: i
    , E% O7 n3 E) U! h) q
    「拉郎配」这个词,《现代汉语词典》就有啊:
      c# y) Q9 L$ A/ q2 W# r
    / T/ V% R3 Y  _0 C
    拉郎配' Q$ f/ c& F/ w/ \2 r, l' a
    lā láng pèi# g" ^1 Q' v" a' r# x6 @" H6 c1 _, k0 I
    封建时代为逃避家中未婚女子被选入宫中,仓促找男子跟她成亲。比喻不顾实际,强行使双方联合或合并。

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?免费注册

    x
  • TA的每日心情
    开心
    11 小时前
  • 签到天数: 2707 天

    [LV.Master]伴坛终老

    发表于 2023-5-3 17:36:36 | 显示全部楼层
    ChatGPT發展甚速。用來辨識漢字。或者要增加設備。
  • TA的每日心情
    开心
    2024-12-23 20:19
  • 签到天数: 442 天

    [LV.9]以坛为家II

    发表于 2023-5-4 12:22:25 | 显示全部楼层
    klwo2 发表于 2023-5-3 17:315 G. u: ]* f$ I  }
    我还是觉得GoldenDict + ChatGPT这个组合算拉郎配' S5 s, C+ f3 H- i# C* [- X# H
    : R0 v5 t  A- J* G
    别的不说,就看下图吧:
    拉郎配
    ) _( K) w3 U0 E

    % k- M7 ?* T3 T' K& Q恰到好处  G0 C& L* \/ p. [+ Y2 |" _9 j" H. F

    8 f4 \0 a- P' @6 V. `  T' P, y* M7 B5 R说明问题
  • TA的每日心情
    无聊
    2022-7-11 10:17
  • 签到天数: 97 天

    [LV.6]常住居民II

    发表于 2023-7-24 10:12:50 | 显示全部楼层
    照着弄完报错啊? 麻烦给看看吧, Z* |2 m( A* n* I/ L" ]9 ?. I
    查询错误: 程序返回,退出代码为 1. Traceback (most recent call last): File "D:\APP\miniconda3\lib\site-packages\urllib3\connectionpool.py", line 700, in urlopen self._prepare_proxy(conn) File "D:\APP\miniconda3\lib\site-packages\urllib3\connectionpool.py", line 994, in _prepare_proxy conn.connect() File "D:\APP\miniconda3\lib\site-packages\urllib3\connection.py", line 364, in connect conn = self._connect_tls_proxy(hostname, conn) File "D:\APP\miniconda3\lib\site-packages\urllib3\connection.py", line 501, in _connect_tls_proxy socket = ssl_wrap_socket( File "D:\APP\miniconda3\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls) File "D:\APP\miniconda3\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock) File "D:\APP\miniconda3\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "D:\APP\miniconda3\lib\ssl.py", line 1040, in _create self.do_handshake() File "D:\APP\miniconda3\lib\ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1131) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\APP\miniconda3\lib\site-packages\requests\adapters.py", line 440, in send resp = conn.urlopen( File "D:\APP\miniconda3\lib\site-packages\urllib3\connectionpool.py", line 813, in urlopen return self.urlopen( File "D:\APP\miniconda3\lib\site-packages\urllib3\connectionpool.py", line 813, in urlopen return self.urlopen( File "D:\APP\miniconda3\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen retries = retries.increment( File "D:\APP\miniconda3\lib\site-packages\urllib3\util\retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/engines/text-davinci-002/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1131)'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\APP\miniconda3\lib\site-packages\openai\api_requestor.py", line 596, in request_raw result = _thread_context.session.request( File "D:\APP\miniconda3\lib\site-packages\requests\sessions.py", line 529, in request resp = self.send(prep, **send_kwargs) File "D:\APP\miniconda3\lib\site-packages\requests\sessions.py", line 645, in send r = adapter.send(request, **kwargs) File "D:\APP\miniconda3\lib\site-packages\requests\adapters.py", line 517, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/engines/text-davinci-002/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1131)'))) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\APP\GoldenDict-addition\mydict.py", line 24, in <module> definition = define(word) File "D:\APP\GoldenDict-addition\mydict.py", line 8, in define response = openai.Completion.create( File "D:\APP\miniconda3\lib\site-packages\openai\api_resources\completion.py", line 25, in create return super().create(*args, **kwargs) File "D:\APP\miniconda3\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( File "D:\APP\miniconda3\lib\site-packages\openai\api_requestor.py", line 288, in request result = self.request_raw( File "D:\APP\miniconda3\lib\site-packages\openai\api_requestor.py", line 609, in request_raw raise error.APIConnectionError( openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/engines/text-davinci-002/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1131)')))
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

    小黑屋|手机版|Archiver|PDAWIKI |网站地图

    GMT+8, 2025-5-11 17:35 , Processed in 0.024947 second(s), 26 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表