掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 2121|回复: 6

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

[复制链接]

该用户从未签到

发表于 2023-5-3 13:11:27 | 显示全部楼层 |阅读模式
9 Z  s. P/ ~3 j0 O& x& M
参考:
. F$ i+ ^* r& D- _4 a- [词典讨论] ChatGPT 用作词典 https://www.pdawiki.com/forum/fo ... hread&tid=48326" F* A! X( Z2 T1 Y+ |
- [经验交流] GoldenDict 有X词典在线版(Python 在线版) https://pdawiki.com/forum/forum.php?mod=viewthread&tid=33203: s+ U! P5 L1 q1 f
- GoldenDict + Python 调用有道翻译[...]  https://pdawiki.com/forum/forum.php?mod=viewthread&tid=21988
) K, w" [1 Z3 E' E5 V% W' @: g- [5.16]goldendict 有道释义 & google翻译 [python] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=26565
1 j9 C/ C0 J) X4 P- 人人词典GoldenDict插件[附python3源码] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=31713
4 V3 }, u$ n* ~9 I9 f4 }4 `- ChatGPT-Next-Web https://github.com/Yidadaa/ChatGPT-Next-Web7 Y8 T0 j7 O1 b( ?0 X  e7 s- O

2 [. H$ {; M! D+ O7 A6 \/ l6 z  J9 m) U5 l9 h0 p4 z
前置条件:
6 F' A% @7 c) l) w% ?/ q1、OpenAI API key https://platform.openai.com/account/api-keys$ ~( w7 U( p# f  n. S
2、Python
) W+ c' j4 }& E* i: X) A
  ^* c+ k0 ~  \7 }3 c* Q% q问:
) q6 L0 F6 z, F2 w5 h
1、写一个 mydict.py,调用 OpenAI API key,实现查询功能。
/ g( o1 E/ h6 e% {% s+ H2、用 GoldenDict 调用 mydict.py。
/ y; P/ S  S3 V
+ `3 W& \9 A! p" O/ c
ChatGPT 回答:. m+ _% O7 D) p' x# \
这里提供一个基于 OpenAI GPT-3 API 的简单 mydict.py 脚本示例,可以通过命令行传入要查询的单词,输出 OpenAI API 返回的结果:
* v$ i% I+ D3 T4 Z" w2 ]4 }% d7 j
```python- J1 \) P4 o3 _( U
import openai
3 ^  x, d' p8 \8 i+ Mimport sys; G+ j% A5 \( s3 a$ A) x

: R8 A/ D7 X# _, C9 f0 F3 {openai.api_key = "YOUR_OPENAI_API_KEY_HERE"# I+ A1 X9 L) g0 y
4 x3 V- z  p5 b# _& v# r
def define(word):
% V. n5 a& h/ D. m" |3 x4 Y    prompt = f"Define '{word}'". _( ^: N; B. g* h: ^/ J9 Y
    response = openai.Completion.create(
1 |! f+ a6 j2 u        engine="text-davinci-002",
& ?, a6 j/ C( o# q6 \        prompt=prompt,
) d8 p2 J, s( y9 p        max_tokens=1024,
2 n3 d' h; |; p: C7 t. q* V        n=1,
$ c# ?2 q- y) u        stop=None,- C  D7 }# b/ c3 B8 {& [: H. l8 y
        temperature=0.5,
. J( {6 x, a0 K2 Y  M. S    )) G: b* r- q+ d* T/ D5 P
# S! ^/ v6 A- ?& x" X& h
    if len(response.choices) > 0:
1 i8 r' M. a( |5 O# \1 n        return response.choices[0].text
9 P% \$ S- ~0 C    else:
; _- @7 ]* [. z  W5 _- p1 i        return "No definition found."
& j4 y5 t8 v* t% g
1 K! L$ c) y: _$ w) Z  dif __name__ == "__main__":" L/ ^; R+ D: @
    word = sys.argv[1]4 w6 P1 x8 W3 |  Q3 `5 @3 a
    definition = define(word)
- K8 k0 L& u1 X8 k2 w    print(definition)
+ s: `4 D, G. t$ H, d2 O9 A```
% R7 `$ O. Z  u% A- f' M: l! e* [% C( ?
将上述脚本保存为 `mydict.py` 文件。5 c. t% v  ]* o5 v$ I$ y
1 [3 O  r  M( Z, ~' v
接下来,在 GoldenDict 中设置该脚本为自定义命令行词典。
/ }/ d$ k# I6 V" D' N+ `7 u  J5 [0 ]  S6 w
1. 打开 GoldenDict,选择 Edit > Dictionaries。
4 p8 b  [5 Q/ V6 T' v: N) ~2. 在 Programs 标签页中,点击 Add 按钮。
0 V6 w3 N0 o) Y3 X( P3. 在 Dialog 标题栏中输入 “MyDict”,在 Command 栏中输入 `python /path/to/mydict.py %GDWORD%`(其中 `/path/to/mydict.py` 为脚本的绝对路径),点击 OK 按钮。
: K: k# Q7 a" u, }3 X% @* j4. 在主界面中,选中一段文本,然后按下 Ctrl+C+C,即可通过 mydict.py 查询该单词的定义。

5 N7 R( I; [6 L1 J( x$ }' H8 e4 o$ w! G# V" a

/ x* k+ X. _3 p+ F+ M8 ]
" Y% b+ x# H, |& N* m: Z* H! ^

评分

1

查看全部评分

本帖被以下淘专辑推荐:

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

    [LV.7]常住居民III

    发表于 2023-5-5 10:16:19 | 显示全部楼层
    本帖最后由 mikeee 于 2023-5-5 11:26 编辑 ' D6 Z; i2 p) {, _' \8 h# @1 y) w
    ' s% X! t  P0 H' ~
    链接: https://pan.baidu.com/s/1HXpt0U3vb7GJDKpBNm_FOw?pwd=4vph 提取码: 4vph 0 h) `  C! `4 D7 a% {3 r( w
    $ [# \" S1 @1 B# f+ j8 [
    这个命令行工具`chatgpt_tr`应该可以设置成在 GoldenDict 里使用。用的是令牌,即是说免费的。: ?' D3 s$ y  {( s$ X8 _2 U

    ! m5 b& O5 P) y+ [chatgpt_tr主要是给命令行翻译用的,但也可以自定义提示Prompt以及用于对话。详细用法参看 ``chatgpt_tr.exe --help``。
    ' [8 y" f9 m" K3 ]; h, W$ W3 t& P2 ]8 J2 j! G6 a
    用法6 J: a. J3 f* n$ e1 C
    ; D$ e) _% }! t
    • 解压
    • 获取access token
      ( P7 {! [0 B/ T" `
      • 登录 chat.openai.com
      • 访问 https://chat.openai.com/api/auth/session
      • 拷 accessToken备用
      • 此操作只需一次。(access token 有效期约14-30天,但内地ip可能需双向提子因为openai和大伙墙的双重封杀,港澳ip则需找未被openai封杀的带理)
        6 ~1 Z2 E) o/ `/ n" a  o! x
    • 命令行下设置环境变量OPENAI_ACCESS_TOKEN,例如cmd下 set OPENAI_ACCESS_TOKEN=eyJhbGc... 或用包内的 chatgpt-tr.bat 或sysdm.cpl设置全局环境变量OPENAI_ACCESS_TOKEN
    • 命令行下,例如WIN cmd回车(或开Windows Terminal或cmder或conemu)
      0 ^4 @3 m$ B+ o- j# p. O& @
      • chatgpt_tr --help # 帮助
      • chatgpr_tr # 翻译系统剪贴板内容
      • chatgpr_tr this is a test # 翻译 this is a test( D- w1 N8 Y7 v5 m2 x6 T
      * Z6 L! v6 P. z4 a/ B

    - }: ^7 X: K( m# V可以利用 bat 自定义提示Prompt各种使用场景(例如`chatgpt_tr.exe -p "列出5个中文同义词"  make sense` )。该工具的python源码核心部分只有十几行。网友有兴趣的话我可以提供python源码。
    $ z9 ^/ n6 E6 }6 a" j
    - r7 [2 k2 t+ o3 B该工具基于某个第三方反向代理,代理挂了或被关闭当然工具也就不能用了。 (实测2023-05-05可用)/ c0 B. D- P0 W. ~5 x/ P
    6 w! l# ?  d6 b" }
    典型输出: 2 L! \- V0 g2 f+ k/ `" d8 |0 j8 }3 d, f
    # chatgpt_tr amazon code whisperer
    ) }) e% A. ]  r7 [8 W. Vamazon code whisperer
    1 ^0 b1 Q/ X9 E% |
    $ g- }' |; S& O! a% a0 f版本1:. h6 f1 f5 S- v  H+ v) }
    亚马逊代码耳语师
    ) N/ P: Y9 h1 A2 b/ ?; ]# O
    ! _, F! l9 a% j& w. Q' ^版本2:
      j/ O; P5 v, n* M+ p亚马逊代码密语者+ F* t0 {3 v  V( p/ R- V( E, z

      P+ v* L* F; O- V版本3:& g! g3 I% T1 Y! v0 ]7 [2 g
    亚马逊代码秘语师/ c% N; Y5 |6 B6 [
    5 Y# m$ v& p4 `5 D( Y% r
    # chatgpt_tr.exe -p "列出5个中文同义词" make sense
    : Q$ K$ ~) z. N4 w8 X% q3 |/ _+ bmake sense
    4 L5 Z* H% A0 V! t! X1 M
    / o: w! u# |% F- e5 |1. 有道理& O- i% R2 c& X' Z! y9 D
    2. 讲得通8 R9 \0 m- T" c. M2 v  p( `2 M
    3. 合理
    ( ^9 I; {, q( K. E3 ~1 S4. 明智
    ; o. H& R; ]! Z) U  \5. 理性5 ?4 E; W, e2 Q. r# j2 E

    7 B: {) F' ^  Z3 l% f1 k% S- {# chatgpt_tr.exe --help( [- v5 y  f! f$ E- @; \2 N

    4 L6 t4 s0 g9 n+ ^6 x7 Q! z Usage: chatgpt_tr.exe [OPTIONS] [TEXT]..., A# n( n# O# H* P4 g+ E2 @

    3 B6 j7 f: Z1 d3 h, i Translate with chatgpt using access token.
      ]# i4 i1 P; v: S9 ? set LOGURU_LEVEL=DEBUG or set LOGLEVEL=10 to turn on debug/verbose mode.
    # y0 Q& f8 _4 P: o) v  t3 Q) c$ }+ f6 R% ?: r8 [. J* |# G, x
    ┌─ Arguments ──────────────────────────────────────────────────────────────────┐
    6 P: c* W+ u, b3 s( ^- p- B│   text      [TEXT]...  Source text. [default: None]                          │5 L) \) g+ [  I5 e
    └──────────────────────────────────────────────────────────────────────────────┘
    * ]1 u  z/ Q" ^; Y┌─ Options ────────────────────────────────────────────────────────────────────┐4 @8 g- H3 w, [4 g1 D) i
    │ --version  -v,-V               Show version info and a list of supported     │
    8 d7 u1 J8 `* l% T│                                languages and exit.                           │
    . z5 Q6 U) M2 Z5 l, S( l' u, v│ --clipb    -c                  Use clipboard content if set or if `text` is  │
    6 ]7 q% q& K- v, g│                                empty.                                        │1 G. |  F: A, T
    │ --to-lang  -t         TEXT     Target language when using the default        │1 w* E1 o$ Q' k! p
    │                                prompt.                                       │) l* E1 \2 {4 O7 s: a
    │                                [default: 中文]                               │
    ( r; U  [- j; Q& r) l. x│ --numb     -n         INTEGER  number of translation variants when using the │# p; _5 H7 o; O1 ]! T
    │                                default prompt.                               │
    2 E: J6 M% Z$ R│                                [default: 3]                                  │7 x( ~; V0 W, P* t8 M& k6 q
    │ --prompt   -p         TEXT     User defined prompt. [default:                │
    ( R4 m" t* }  D3 z- J% F- m/ e│                                '将以下内容内容翻成 to_lang                   │
    ! S4 x' V/ \" |! D│                                (中文),不要自由发挥,列出 numb (3) 个版本:  │
    4 W5 H" M: p9 K+ X! _/ M│                                \n']                                          │
    0 X# G1 j' q# n) Z& ]2 g│ --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这个组合算拉郎配
    * e' U' g) A) R2 g$ b6 l8 e8 ]' y( _# D# l  l
    别的不说,就看下图吧:
    , R9 b2 C) o( \7 Y; _/ o3 M( q% V/ j

    6 Z7 O( Y: k$ L
    , i% `9 L. [; d4 zDefine指令不支持中文 用中文吧,它给的解释还是错的! u  v5 O+ W7 i# L# f

    ) {+ T/ G% e9 I. z# i9 h7 i* u  t9 n/ r
    「拉郎配」这个词,《现代汉语词典》就有啊:: [5 `3 R! H9 ?* {# G+ Q  e

    7 J; }' E& h, K0 i
    拉郎配
    2 d7 O+ |& B3 `5 d( G8 q- O' vlā láng pèi
    , g* \, D- [* |, @1 X封建时代为逃避家中未婚女子被选入宫中,仓促找男子跟她成亲。比喻不顾实际,强行使双方联合或合并。

    本帖子中包含更多资源

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

    x
  • TA的每日心情
    开心
    昨天 06:07
  • 签到天数: 2392 天

    [LV.Master]伴坛终老

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

    [LV.9]以坛为家II

    发表于 2023-5-4 12:22:25 | 显示全部楼层
    klwo2 发表于 2023-5-3 17:31
    & v2 O- Z, w7 \/ ?. P2 N我还是觉得GoldenDict + ChatGPT这个组合算拉郎配: f4 x) @2 u2 V
    . O# q4 w( P5 ^1 J' X6 E* H" V, b
    别的不说,就看下图吧:
    拉郎配
    : j# n) y9 r7 ], g- g+ u% ?; U
    7 ~4 F; {( f& {; K: h; g- Z2 Y
    恰到好处
    0 U. |" m" R5 {; e8 ~! }
    , \$ k* p$ u" Q' N8 f说明问题
  • TA的每日心情
    无聊
    2022-7-11 10:17
  • 签到天数: 97 天

    [LV.6]常住居民II

    发表于 2023-7-24 10:12:50 | 显示全部楼层
    照着弄完报错啊? 麻烦给看看吧# a$ U- C+ L* N) @! T  |7 J$ W4 d) _
    查询错误: 程序返回,退出代码为 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, 2024-6-14 00:54 , Processed in 0.066557 second(s), 12 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2023, Tencent Cloud.

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