掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 2015|回复: 6

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

[复制链接]

该用户从未签到

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

' d) H4 j: V1 ^参考:( R: v2 V( ]' P/ L+ E
- [词典讨论] ChatGPT 用作词典 https://www.pdawiki.com/forum/fo ... hread&tid=48326
; }* x6 y6 Y9 c% k* C- [经验交流] GoldenDict 有X词典在线版(Python 在线版) https://pdawiki.com/forum/forum.php?mod=viewthread&tid=33203
1 ^9 k; C, \+ O0 e, X) W- GoldenDict + Python 调用有道翻译[...]  https://pdawiki.com/forum/forum.php?mod=viewthread&tid=21988% a; G  a* u) W8 n% m' ?) E1 x- f
- [5.16]goldendict 有道释义 & google翻译 [python] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=26565) f  z5 W" f. N+ X* {( `
- 人人词典GoldenDict插件[附python3源码] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=31713
0 Y6 u6 r3 r& A, L- ChatGPT-Next-Web https://github.com/Yidadaa/ChatGPT-Next-Web9 q9 m+ O) V( R5 n" J" t3 P+ ~

. m0 n& E2 P" d1 [6 {: P3 B! Q
  G6 {. U- H. `$ y% N) w前置条件:
7 n$ J# Y0 \5 P$ I1、OpenAI API key https://platform.openai.com/account/api-keys
) M8 x  l! ~. [* u9 a+ @6 e2、Python
! W+ l  y& x# O& c# B' v+ o; Z) L9 @+ s, X& l' v8 y" i
问:
# p! T& y+ o4 r/ B0 l- g5 e& _
1、写一个 mydict.py,调用 OpenAI API key,实现查询功能。) g* N/ l  S* S' Y6 q$ X1 U
2、用 GoldenDict 调用 mydict.py。

& {% g  [% }, f9 o+ }( F' s) D6 p$ M2 k+ F
ChatGPT 回答:
# D. N" K# [! M. D
这里提供一个基于 OpenAI GPT-3 API 的简单 mydict.py 脚本示例,可以通过命令行传入要查询的单词,输出 OpenAI API 返回的结果:0 m+ p- A' F# k' N

" n$ g$ K% N7 i; F% G3 n```python
! Y- f' x+ I& t/ {2 gimport openai
: \$ ^! ~- ^) E" v/ B1 l) Bimport sys6 ?* |. g$ L. a( x2 z# n
) l& f' |1 h7 N! ~" g0 O' c  f: R
openai.api_key = "YOUR_OPENAI_API_KEY_HERE"
( U5 [9 M5 j4 i9 W/ `* E! D
: W7 A3 G8 U* p- Z  z! }: Vdef define(word):( t" [! M! e$ e( E0 R
    prompt = f"Define '{word}'"$ u- S3 U( I; d" `0 s% t
    response = openai.Completion.create(0 M" d+ U9 c( g, Z' ]
        engine="text-davinci-002",
1 a. [7 c+ K/ t6 }& _' W        prompt=prompt,
# T& _( w/ W- |4 Y0 w" T        max_tokens=1024,0 G" Y1 U: y( B
        n=1,
1 z* k6 i7 Z2 m9 [0 z        stop=None,
. z9 a& @4 D1 j+ D/ G        temperature=0.5,$ ]0 O" y8 w* U" p
    )
4 W( K1 a' u/ o
( V. I8 ^2 J6 v$ t" f5 b3 K3 \    if len(response.choices) > 0:) |1 b" [# h# M) Z8 Y6 S) V1 }8 g, f6 }
        return response.choices[0].text6 X9 G9 c$ Z' p0 q$ K2 b
    else:
" @. B3 s5 R( y- X  |        return "No definition found."
7 l# n& q' @9 b0 ?* A$ y7 c
  {7 v4 D# j* F) c' vif __name__ == "__main__":
/ u  h% ?( a/ S' _! o. Z    word = sys.argv[1]
# X' [7 |4 D  U% H) m  l. k2 M    definition = define(word)* H# K! f% ^  ]! J; ?1 C1 F
    print(definition)# Z' q7 k, ^# O  x
```
* I+ d4 G  H& `6 s& V5 f6 I# J4 i" p
将上述脚本保存为 `mydict.py` 文件。0 w) L" E) u' C+ C1 |- f% T' p

1 O$ |' X- J  h$ v( Z5 V6 j7 q5 S+ M  `接下来,在 GoldenDict 中设置该脚本为自定义命令行词典。/ h: A. {- @6 n0 h0 M
& R+ j& I1 u% Q" c
1. 打开 GoldenDict,选择 Edit > Dictionaries。3 k0 F. f% L& U
2. 在 Programs 标签页中,点击 Add 按钮。5 t8 T: k# S. y
3. 在 Dialog 标题栏中输入 “MyDict”,在 Command 栏中输入 `python /path/to/mydict.py %GDWORD%`(其中 `/path/to/mydict.py` 为脚本的绝对路径),点击 OK 按钮。# z; }$ c+ A9 D" d- P5 u. C# G9 [" o
4. 在主界面中,选中一段文本,然后按下 Ctrl+C+C,即可通过 mydict.py 查询该单词的定义。
& H4 Q9 X+ f. G. C- C! L

5 b- y$ v; ?$ i. i# q7 |; x$ b' u) x  a6 F; V
6 I& y' ]& H  a. k( F+ S

评分

1

查看全部评分

本帖被以下淘专辑推荐:

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

    [LV.7]常住居民III

    发表于 2023-5-5 10:16:19 | 显示全部楼层
    本帖最后由 mikeee 于 2023-5-5 11:26 编辑 : \9 n: Z# Z2 Y! S/ G- j) H
    ( F2 k+ w) e4 V6 B: E6 n* |
    链接: https://pan.baidu.com/s/1HXpt0U3vb7GJDKpBNm_FOw?pwd=4vph 提取码: 4vph
    9 X, n- h! l& J" m$ v
    , h( }. Z/ @7 H& {* m( v这个命令行工具`chatgpt_tr`应该可以设置成在 GoldenDict 里使用。用的是令牌,即是说免费的。
    7 g" `' F  \+ \; \
    1 o1 Y. z& y* y+ [+ s+ E; y/ f0 A& Z% Vchatgpt_tr主要是给命令行翻译用的,但也可以自定义提示Prompt以及用于对话。详细用法参看 ``chatgpt_tr.exe --help``。- q7 m8 s2 F' q: W+ \
    ! e, Y' C# H4 j9 p. g
    用法, {7 d- A$ X( o& n
    4 e' ~8 Z3 |' ]/ c5 L, x( b, o
    • 解压
    • 获取access token
      2 H% h  B; a* n: Y
      • 登录 chat.openai.com
      • 访问 https://chat.openai.com/api/auth/session
      • 拷 accessToken备用
      • 此操作只需一次。(access token 有效期约14-30天,但内地ip可能需双向提子因为openai和大伙墙的双重封杀,港澳ip则需找未被openai封杀的带理)
        + r  d$ `& m/ V  H
    • 命令行下设置环境变量OPENAI_ACCESS_TOKEN,例如cmd下 set OPENAI_ACCESS_TOKEN=eyJhbGc... 或用包内的 chatgpt-tr.bat 或sysdm.cpl设置全局环境变量OPENAI_ACCESS_TOKEN
    • 命令行下,例如WIN cmd回车(或开Windows Terminal或cmder或conemu)
      , O  m. o& ^7 ]" v! [3 S/ F7 {
      • chatgpt_tr --help # 帮助
      • chatgpr_tr # 翻译系统剪贴板内容
      • chatgpr_tr this is a test # 翻译 this is a test
        5 ^  h' E* _+ [" m7 x# S
      / S" L/ m/ ^$ j. K0 s

    - l5 V/ ?2 w! q7 u8 o/ n- r! B可以利用 bat 自定义提示Prompt各种使用场景(例如`chatgpt_tr.exe -p "列出5个中文同义词"  make sense` )。该工具的python源码核心部分只有十几行。网友有兴趣的话我可以提供python源码。
    5 M, t  I. t: o; ]5 |: }7 g  B' m6 y, _: L
    该工具基于某个第三方反向代理,代理挂了或被关闭当然工具也就不能用了。 (实测2023-05-05可用)& ]5 b/ m$ ~- k8 Z3 G; w- I

    % e. a8 d: X9 t- X典型输出:
    % d8 q9 U7 y: O: R4 O+ z: l. A5 h
    # chatgpt_tr amazon code whisperer, u9 i: l8 S0 \) e8 V0 r
    amazon code whisperer
    & _" `7 c6 E! }' Q1 E. K
    % F9 s: a) L6 R3 [版本1:
    : r/ G! {, }: o* [0 E( J亚马逊代码耳语师; p- F2 t3 m( l0 y- V

    8 V: c0 z6 \+ A版本2:# d9 k7 y! b/ e% |
    亚马逊代码密语者
    " R  y+ l( m! C2 L: |% m* C6 K- x" z% ~5 o' C' [0 |- c
    版本3:/ a5 \; K/ Q% r
    亚马逊代码秘语师2 R7 P" E& a& w' d( c1 G

    4 f9 Z9 Z7 r1 D3 u# chatgpt_tr.exe -p "列出5个中文同义词" make sense
    9 w+ ?2 F$ m3 }; Cmake sense& P# c8 }+ H: z5 l3 z% V/ s6 g
    ; k; H. R* A* i
    1. 有道理1 P& Q: B8 o  t
    2. 讲得通
    , b- c0 o- R% P, y/ C& O# S3. 合理
    3 b# ?: o# C( |4. 明智6 m- ~8 E2 }9 |' I: V* z# D( {
    5. 理性
    9 H7 S  s, n2 W% f& n9 v' o' H. @' @, r3 g) z4 M5 H+ E9 k* ~
    # chatgpt_tr.exe --help' j3 ~, V3 N8 z5 x+ x+ {0 `

    0 J' T7 A: m2 `0 Y0 A+ u Usage: chatgpt_tr.exe [OPTIONS] [TEXT]...1 {+ g& Q$ Y' t& B" _9 _
    ) v1 u5 V; w/ Q. x2 X) N* o! z
    Translate with chatgpt using access token." h0 `) d  N3 J) T3 v: a1 H+ d
    set LOGURU_LEVEL=DEBUG or set LOGLEVEL=10 to turn on debug/verbose mode.
    % s% v1 N$ @% c* r7 x* G: a
    : F+ f: {" x; e1 x5 u. c3 l8 c, N┌─ Arguments ──────────────────────────────────────────────────────────────────┐
    * n6 Z( X# v! ~" [1 J8 I│   text      [TEXT]...  Source text. [default: None]                          │
    3 X# Z* F; [& r. j7 e3 Y└──────────────────────────────────────────────────────────────────────────────┘
    ! l: T- R4 s  b# S8 R8 A: ~┌─ Options ────────────────────────────────────────────────────────────────────┐' r# J1 _! y, `
    │ --version  -v,-V               Show version info and a list of supported     │
    1 P6 X: O) m: M│                                languages and exit.                           │
    ; f. u# V* U  a9 D4 y; _/ K0 @│ --clipb    -c                  Use clipboard content if set or if `text` is  │
    ( g9 A- w! G+ j│                                empty.                                        │, ]: E- {6 M1 o% N- u6 B7 I. j7 C
    │ --to-lang  -t         TEXT     Target language when using the default        │
    ) p/ D* N. L- V0 ?+ @│                                prompt.                                       │7 C' w9 l& C! }7 W& i* g, g3 g
    │                                [default: 中文]                               │2 g' o% x2 Q5 ~2 H/ D6 B5 t, N) k
    │ --numb     -n         INTEGER  number of translation variants when using the │
    ! ~, l5 `, W& p, E# Q/ ?│                                default prompt.                               │
    3 d& ^1 q' x$ x│                                [default: 3]                                  │6 N/ f  |5 }& Q! s1 f2 Y( u3 H
    │ --prompt   -p         TEXT     User defined prompt. [default:                │0 s$ p  q$ _. t3 ~1 w* w
    │                                '将以下内容内容翻成 to_lang                   │
    " R6 g2 Q* e6 q│                                (中文),不要自由发挥,列出 numb (3) 个版本:  │/ I8 ]* Z/ Z. M7 f1 q% q! i0 o
    │                                \n']                                          │
    3 y7 q2 C2 }- d/ T6 Y│ --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这个组合算拉郎配" f! c: z0 i$ b( {8 ?* W' q$ a
    " f. J% _$ U; W* P7 w( g* v
    别的不说,就看下图吧:
    % d3 L& _/ z( y# a% ~% R3 [4 t) W' P% l8 Z
    & O( k& s+ a" |1 O( j

    3 ?4 k% a5 ?& y0 G% @; ~$ e3 GDefine指令不支持中文 用中文吧,它给的解释还是错的
    ' B& N* A/ n/ V; [% @
    * _; M. ~% y; s4 j& F0 u% N
    2 C6 E. K% v. U! M4 p6 @. _( e「拉郎配」这个词,《现代汉语词典》就有啊:
    0 w& o" W* j. i! R: l5 w+ E% q1 k, |( A. Y. V# a: H3 ?+ W" g
    拉郎配' K% h  [. i4 d) S
    lā láng pèi1 J* m% ]1 u1 c5 s
    封建时代为逃避家中未婚女子被选入宫中,仓促找男子跟她成亲。比喻不顾实际,强行使双方联合或合并。

    本帖子中包含更多资源

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

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

    [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:310 {0 A- g3 j4 g( u+ z
    我还是觉得GoldenDict + ChatGPT这个组合算拉郎配$ y/ m5 J7 u0 Q" ~. d- X; W5 K
    8 I. ^5 q- V5 c" w* `
    别的不说,就看下图吧:
    拉郎配

    / a3 ~" _! ^8 D* W9 y& X# @3 ?* V9 t$ F
    恰到好处
    ( N% y6 n' Y% K* L- w
    " O# n+ U7 w4 L( y2 c说明问题
  • TA的每日心情
    无聊
    2022-7-11 10:17
  • 签到天数: 97 天

    [LV.6]常住居民II

    发表于 2023-7-24 10:12:50 | 显示全部楼层
    照着弄完报错啊? 麻烦给看看吧
    4 V+ _" w+ {" v; K  R: F/ e2 N查询错误: 程序返回,退出代码为 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-5-17 11:23 , Processed in 0.062030 second(s), 12 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2023, Tencent Cloud.

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