掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 3725|回复: 6

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

[复制链接]

该用户从未签到

发表于 2023-5-3 13:11:27 | 显示全部楼层 |阅读模式
2 `( T- x3 {% `2 u
参考:
4 W+ Y1 t0 q8 B- [词典讨论] ChatGPT 用作词典 https://www.pdawiki.com/forum/fo ... hread&tid=483264 a& @% v' X8 q+ F$ S
- [经验交流] GoldenDict 有X词典在线版(Python 在线版) https://pdawiki.com/forum/forum.php?mod=viewthread&tid=33203
" r; I- t/ ~. ~) i0 u- GoldenDict + Python 调用有道翻译[...]  https://pdawiki.com/forum/forum.php?mod=viewthread&tid=21988! c8 c, C1 m2 p6 {  x
- [5.16]goldendict 有道释义 & google翻译 [python] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=26565
! y# S: \3 e4 D8 i+ V# [" g* p. n9 z- 人人词典GoldenDict插件[附python3源码] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=31713
3 u* r% T, o2 O8 P- ChatGPT-Next-Web https://github.com/Yidadaa/ChatGPT-Next-Web) k& K1 b. }  W; S4 }* I

- r: t" n% a$ T3 I- ^+ v0 \. @- [
前置条件:/ |  B3 y! v; |+ f. U" V
1、OpenAI API key https://platform.openai.com/account/api-keys
$ _. b9 B8 Y3 R' K0 r7 U: b0 `2、Python. h1 J  N" n5 a! n9 ~0 V( H

- D% J0 n* H  [( b/ o* L4 S6 a问:
! ]! \* h3 `! Z& B- \4 c
1、写一个 mydict.py,调用 OpenAI API key,实现查询功能。
3 k8 t( `/ E" H  d! t$ \4 m2、用 GoldenDict 调用 mydict.py。

+ v( u& B* f+ [5 d" Z/ E* H6 d& C0 W, W1 r' ?
ChatGPT 回答:$ \# F) g( y2 L
这里提供一个基于 OpenAI GPT-3 API 的简单 mydict.py 脚本示例,可以通过命令行传入要查询的单词,输出 OpenAI API 返回的结果:
9 O$ s4 \' D" c7 F
) V0 i3 p9 V& O5 i```python
" X) s5 `, n* v/ x8 d9 Y9 kimport openai
4 V- }7 a. L8 g( ]import sys8 I6 O6 Y. }5 t3 M9 B2 c
. F% k. l7 P2 ^. R8 n4 R
openai.api_key = "YOUR_OPENAI_API_KEY_HERE"
. }8 X/ [+ _1 ~; @3 D5 f) Y1 [( f1 ~
def define(word):4 k& `+ D  w, S' w
    prompt = f"Define '{word}'"; M9 W+ j, J: H* O% k/ l6 i
    response = openai.Completion.create(& ~( n1 v5 w2 b2 P
        engine="text-davinci-002",
9 t& W0 H0 A* {  x0 J; `( h; |! e        prompt=prompt,
! {& a4 m( ?$ q& t2 N0 l$ E% B+ r        max_tokens=1024,
. \* O( G: C- b2 P        n=1,& T, i1 g1 }$ J* |6 G
        stop=None,
/ C( R. Q+ E# n. R0 Q/ s        temperature=0.5,
3 E5 t, c7 q! m; b- m    )  l2 f, T; h- }# _
' k: G) ]/ E- s2 h! t1 v
    if len(response.choices) > 0:2 k$ s9 V; l% q4 S# v- g, e
        return response.choices[0].text' p8 f. h& }1 O1 \
    else:
: a3 l" x" n( |+ l8 _  t3 U        return "No definition found."
- T+ l& \' J% r7 v( _, O$ g( o/ h8 \+ E0 N
if __name__ == "__main__":
4 J' n7 S0 D4 E' e5 c2 X    word = sys.argv[1], |8 E/ v" L  g' W; N; m
    definition = define(word)
1 x3 I" `$ r: O4 x9 W    print(definition)5 C: ^3 Z% h6 f! W+ Y
```* `# T5 I/ f( i3 c' n& G1 V/ N9 K

6 y9 E, `. \8 B! q将上述脚本保存为 `mydict.py` 文件。, j4 K1 N0 b) z; {

; d. A  ~# u3 m) Y$ {  G. }( s) R接下来,在 GoldenDict 中设置该脚本为自定义命令行词典。5 z  Q6 }9 ?/ ^+ l  \( l/ P- h
  I& a2 x. k3 ~/ f" B
1. 打开 GoldenDict,选择 Edit > Dictionaries。
4 g, A5 g  C* P8 p9 o: f2. 在 Programs 标签页中,点击 Add 按钮。9 F) V& S5 n3 A
3. 在 Dialog 标题栏中输入 “MyDict”,在 Command 栏中输入 `python /path/to/mydict.py %GDWORD%`(其中 `/path/to/mydict.py` 为脚本的绝对路径),点击 OK 按钮。
3 d% |! O% [5 k# s, G4. 在主界面中,选中一段文本,然后按下 Ctrl+C+C,即可通过 mydict.py 查询该单词的定义。
+ r0 H1 _4 P0 f  l% L4 A+ ?9 i  C
/ E* z, v3 N. S3 h( p
3 [  ]  {# T- c- O

8 h  ], L; |- x4 B8 v/ c

评分

1

查看全部评分

本帖被以下淘专辑推荐:

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

    [LV.7]常住居民III

    发表于 2023-5-5 10:16:19 | 显示全部楼层
    本帖最后由 mikeee 于 2023-5-5 11:26 编辑 5 }4 @! ^9 R) F* }

    6 q! F: _1 K0 K 链接: https://pan.baidu.com/s/1HXpt0U3vb7GJDKpBNm_FOw?pwd=4vph 提取码: 4vph
    : U* x& y& i$ t" w0 \+ l
    7 k$ C, G8 Z" v* K8 u( i( `, C7 {这个命令行工具`chatgpt_tr`应该可以设置成在 GoldenDict 里使用。用的是令牌,即是说免费的。
    ! |# W! Y; x" ]: `- ^& @% v1 x7 J+ [; F% _# @$ i$ Y5 K2 _
    chatgpt_tr主要是给命令行翻译用的,但也可以自定义提示Prompt以及用于对话。详细用法参看 ``chatgpt_tr.exe --help``。
    7 U3 a$ f/ {! f2 |7 k: ?
    ( K- m3 C- F2 j7 N用法5 \! q6 P; m! H, r

      p) j. H6 v! x& j
    • 解压
    • 获取access token
      & L% ~5 `" a  [+ Y
      • 登录 chat.openai.com
      • 访问 https://chat.openai.com/api/auth/session
      • 拷 accessToken备用
      • 此操作只需一次。(access token 有效期约14-30天,但内地ip可能需双向提子因为openai和大伙墙的双重封杀,港澳ip则需找未被openai封杀的带理)
        ' Y) }. W* ?+ G! }  s% S% ~( _2 Q
    • 命令行下设置环境变量OPENAI_ACCESS_TOKEN,例如cmd下 set OPENAI_ACCESS_TOKEN=eyJhbGc... 或用包内的 chatgpt-tr.bat 或sysdm.cpl设置全局环境变量OPENAI_ACCESS_TOKEN
    • 命令行下,例如WIN cmd回车(或开Windows Terminal或cmder或conemu)
      5 j) @* S" V* L  t5 u
      • chatgpt_tr --help # 帮助
      • chatgpr_tr # 翻译系统剪贴板内容
      • chatgpr_tr this is a test # 翻译 this is a test+ i; V- B' L. P" d1 I' M- m4 r
      9 Q) s# J4 M; E4 d' K0 S- |
    7 S! S& ^, `4 ]9 ?3 ]! G7 n
    可以利用 bat 自定义提示Prompt各种使用场景(例如`chatgpt_tr.exe -p "列出5个中文同义词"  make sense` )。该工具的python源码核心部分只有十几行。网友有兴趣的话我可以提供python源码。
    9 ^9 c2 O: H3 C6 y+ `
    - j' A3 {# C8 X2 |- t% `该工具基于某个第三方反向代理,代理挂了或被关闭当然工具也就不能用了。 (实测2023-05-05可用)
    - d2 T$ K* U/ ^# v( P+ |% ?; S
      H0 B6 n, E) ?3 j: d& o典型输出:
      P3 `! v. m( w# p9 m& L
    # chatgpt_tr amazon code whisperer
    - @8 Q) S* ]" |! ^8 ~! [) Xamazon code whisperer
    * i- }9 `$ `8 [$ i) T% i. x% B( B+ j$ d0 G+ t1 U
    版本1:# `. B( M) e& y' l. r1 Y  [; M
    亚马逊代码耳语师/ I3 q- p% F+ x* ]  c% L7 ]1 y2 Y# U
    ( r, Z3 ~* v+ T& l5 x( m1 ~
    版本2:- C- a1 y- Y; V! v2 M
    亚马逊代码密语者4 {, `5 [- d5 R1 u
    7 l0 l6 s+ A( v/ }
    版本3:
    + s. A* M# q$ r/ W$ Z+ E5 N) f! N4 y, o亚马逊代码秘语师9 z  ?+ f% e  {% t! K5 X# H

    8 E, C; k7 g2 L. \9 K# chatgpt_tr.exe -p "列出5个中文同义词" make sense
    4 s6 \& W4 \2 lmake sense' e+ Y$ L7 ?+ o
    ) _$ r- ?( G/ F5 D
    1. 有道理% O+ x2 w+ p$ s/ h
    2. 讲得通0 p9 c  h- O3 O; x8 P% P
    3. 合理5 L6 E. ^) |/ S; L, }4 S
    4. 明智
    4 t  b% z/ ?+ ~3 v  a2 n7 Z5. 理性
    . T3 f; t7 Y! m( [  d4 \- n" H: F
    # chatgpt_tr.exe --help
    8 [: S- S3 {$ t7 u. m. X% P- p
    ; t3 E7 z+ {9 L2 n Usage: chatgpt_tr.exe [OPTIONS] [TEXT]...
    . r0 D) m" P& F" d; \4 G' N$ t! @1 I6 ~$ S
    Translate with chatgpt using access token.
    " x5 f$ \: q% O set LOGURU_LEVEL=DEBUG or set LOGLEVEL=10 to turn on debug/verbose mode.8 M  H6 x0 Q: G% w

    ' i. e' _+ l, _┌─ Arguments ──────────────────────────────────────────────────────────────────┐
    $ }/ C5 g/ J9 J- P# C) c! T│   text      [TEXT]...  Source text. [default: None]                          │/ w  M& t, X0 B2 @4 Q) t0 E
    └──────────────────────────────────────────────────────────────────────────────┘$ J: {& V) @6 k& k. O7 ^- i! O5 J
    ┌─ Options ────────────────────────────────────────────────────────────────────┐! M* O' K* x! X, h3 e
    │ --version  -v,-V               Show version info and a list of supported     │
    : r9 J& a4 ?. R% |7 b* b│                                languages and exit.                           │* X$ v- I* J7 t7 y
    │ --clipb    -c                  Use clipboard content if set or if `text` is  │
    4 l! G4 w' O+ D) U" Q7 ]- W( T" t│                                empty.                                        │) ^9 H; p4 a7 M9 k/ u+ C
    │ --to-lang  -t         TEXT     Target language when using the default        │
    ! e; k4 \1 H; N( C6 ^8 b. _, @│                                prompt.                                       │
    9 t& D0 m3 J& i( a) S2 n: ?5 p/ t│                                [default: 中文]                               │
    % @* t4 z5 ]# u& X) Z│ --numb     -n         INTEGER  number of translation variants when using the │
    ! I8 a  z; i% J│                                default prompt.                               │
    + ]; }" q$ y: x# C9 {1 P│                                [default: 3]                                  │  x. S5 V# |( \4 y+ x! R
    │ --prompt   -p         TEXT     User defined prompt. [default:                │( \% d. E( }+ O) K( j
    │                                '将以下内容内容翻成 to_lang                   │/ F2 ]* V* {  i* o- \) {
    │                                (中文),不要自由发挥,列出 numb (3) 个版本:  │& p& Z5 B4 Y% k& w; D( I
    │                                \n']                                          │
    ' @! a4 }' v) N. m│ --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这个组合算拉郎配+ O. F* o0 P7 T2 r: b4 _( R! f
    5 ^8 }" C& u8 Y% ]' n# m
    别的不说,就看下图吧:/ k) }) \& |1 J
    * E/ a# {+ s  c7 w9 y1 L0 O+ S4 e7 E6 U
    7 Q; q" I, T0 U! G* R
    1 B$ Z. L0 {/ c
    Define指令不支持中文 用中文吧,它给的解释还是错的) W; P0 F" n; n
    : T% ]" z3 b3 p+ Y( m1 I/ C
    1 `$ E# E- V# R& H! _# l+ [3 R
    「拉郎配」这个词,《现代汉语词典》就有啊:
    0 {- M$ x) q0 B% {3 d$ v; H
    % x/ R0 c- m, l1 r% I. i
    拉郎配# O3 k' o1 D' L3 H/ G& {7 @8 j
    lā láng pèi" p7 [1 F1 G7 ?* R
    封建时代为逃避家中未婚女子被选入宫中,仓促找男子跟她成亲。比喻不顾实际,强行使双方联合或合并。

    本帖子中包含更多资源

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

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

    [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:31) [$ @- K: v3 G
    我还是觉得GoldenDict + ChatGPT这个组合算拉郎配
    , ?6 {' T! z, X3 P5 l0 T, e# M. X( N( M
    别的不说,就看下图吧:
    拉郎配
    2 i- c/ c0 _, ]) {# [
      `: z( b$ W) h
    恰到好处
    8 V) [3 G% U4 I3 B( P' X2 j2 S+ ~+ I. W9 |
    说明问题
  • TA的每日心情
    无聊
    2022-7-11 10:17
  • 签到天数: 97 天

    [LV.6]常住居民II

    发表于 2023-7-24 10:12:50 | 显示全部楼层
    照着弄完报错啊? 麻烦给看看吧1 m/ p- N+ W8 o9 D9 T% b0 h% p. 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-3 05:54 , Processed in 0.025758 second(s), 29 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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