掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 1940|回复: 6

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

[复制链接]

该用户从未签到

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

3 A: X+ I7 e  a. U3 k参考:, Y8 a: j) h/ @  e
- [词典讨论] ChatGPT 用作词典 https://www.pdawiki.com/forum/fo ... hread&tid=48326: {% t# P, J( j, b" Y
- [经验交流] GoldenDict 有X词典在线版(Python 在线版) https://pdawiki.com/forum/forum.php?mod=viewthread&tid=33203
! t' q% U" ~% s  v- n. S- GoldenDict + Python 调用有道翻译[...]  https://pdawiki.com/forum/forum.php?mod=viewthread&tid=21988
( H$ [! h5 [  }' ]3 ^. |- [5.16]goldendict 有道释义 & google翻译 [python] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=26565
1 S4 y. m' E* r7 e- 人人词典GoldenDict插件[附python3源码] https://pdawiki.com/forum/forum.php?mod=viewthread&tid=317136 \$ ^  j, ]0 ]; ~! A$ f6 J
- ChatGPT-Next-Web https://github.com/Yidadaa/ChatGPT-Next-Web
/ w: a- x5 W$ h7 }% z6 b$ x
. H& d) i. i( H# O
+ ]2 j9 x7 S9 E: c+ p+ }' v) b# ^前置条件:
$ n2 O0 f; T& \6 u0 k1、OpenAI API key https://platform.openai.com/account/api-keys$ j9 K. g$ |' _. f$ G4 ~
2、Python
4 ^6 g' V; E9 f6 w/ |; P
* x5 U. T, ^3 `4 S$ H; Z  T! |& U. X, H问:8 ?6 ?# N& R* a/ M) P  R* A
1、写一个 mydict.py,调用 OpenAI API key,实现查询功能。4 g1 H) k! _$ J: a5 S
2、用 GoldenDict 调用 mydict.py。

  ~: `) E7 x7 h% J1 K2 {5 O' z, N+ k" _$ w: ~9 |; i
ChatGPT 回答:
9 c/ Y! @' C: V/ B9 x
这里提供一个基于 OpenAI GPT-3 API 的简单 mydict.py 脚本示例,可以通过命令行传入要查询的单词,输出 OpenAI API 返回的结果:
! {8 ~# |8 O& g( \
; W( D6 W4 i1 O8 M) h```python
# f( l3 b$ J& gimport openai
4 S1 Y- e0 v& ^: X! l4 Yimport sys
* c9 f8 f/ w4 w0 f5 q- k8 g
3 R! e# T6 f2 `  ~" Y: W7 q- ?& }! i9 zopenai.api_key = "YOUR_OPENAI_API_KEY_HERE"
5 D2 T0 x: C! |; Y$ o3 I2 U% I9 r8 Y  e: O2 G
def define(word):$ q. M: q1 D, v- Q; ~& c3 S0 L' f
    prompt = f"Define '{word}'"
8 u: i3 B4 ?: y' K/ b/ O; j    response = openai.Completion.create($ I% ?! M$ M5 t( i6 \% R
        engine="text-davinci-002",( Y' c  d" i: w7 O2 r
        prompt=prompt,
, N, d; \" ?( c9 |) Q0 \        max_tokens=1024,
9 l$ ]2 G3 K+ p, j2 D8 f        n=1,0 F. z3 j1 i: z5 K: X! X
        stop=None,5 p5 T5 V0 J2 _* P/ x, v
        temperature=0.5,
( V1 g5 W) n' X, t    )
" m, |/ Z/ o) s& }
; ^! x* D) Q" |: L/ w# J1 u& F    if len(response.choices) > 0:
7 I: R$ L  o* ~6 q  ^$ e( j* {) g        return response.choices[0].text# k) J% N' y/ R! r
    else:8 F( w+ ~6 ]7 r, {  h9 o
        return "No definition found."
/ |6 u0 C; Q4 q
$ ^6 m8 x) J1 @( g, Y3 U4 u% cif __name__ == "__main__":
9 R5 a/ p% m! ~+ Q) T  H0 ?5 G    word = sys.argv[1]. l6 ~) n3 m2 ~9 c; l3 [' V4 S% b0 I
    definition = define(word)
! r: Z) p- u% A  N: ~: w    print(definition)
" i$ S- j7 _2 x2 R9 B+ i9 }```
4 a2 \5 k( n1 [
; r3 v. x5 F! T" R# r9 ]- j5 s. e将上述脚本保存为 `mydict.py` 文件。
: p& q0 |3 f$ W( [3 i5 y% w2 i; z6 r4 o7 M% l, @
接下来,在 GoldenDict 中设置该脚本为自定义命令行词典。
: ^% x! i8 x( ?# O& k! w& _) n! D2 z9 q8 U
1. 打开 GoldenDict,选择 Edit > Dictionaries。
; w, Q+ n5 Z; O! W( C5 V5 b3 s2. 在 Programs 标签页中,点击 Add 按钮。% _, p8 u: c+ L& u
3. 在 Dialog 标题栏中输入 “MyDict”,在 Command 栏中输入 `python /path/to/mydict.py %GDWORD%`(其中 `/path/to/mydict.py` 为脚本的绝对路径),点击 OK 按钮。+ D' R7 w. I, q8 ]; ~. d
4. 在主界面中,选中一段文本,然后按下 Ctrl+C+C,即可通过 mydict.py 查询该单词的定义。

/ _# O9 \; U- R# K9 A  Z. |' V- Y! z. u  t6 m' r

- z; t3 ~1 Z& a5 E; R' o3 Z2 ?: ]( ~, o5 V

评分

1

查看全部评分

本帖被以下淘专辑推荐:

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

    [LV.7]常住居民III

    发表于 2023-5-5 10:16:19 | 显示全部楼层
    本帖最后由 mikeee 于 2023-5-5 11:26 编辑 1 a, _! v9 K, G4 ^+ Q( n) e
    # U3 M9 c) q0 H: R% S+ f
    链接: https://pan.baidu.com/s/1HXpt0U3vb7GJDKpBNm_FOw?pwd=4vph 提取码: 4vph 0 v8 [$ C4 r7 O! Q6 Y
    " \) v' {2 Q, F
    这个命令行工具`chatgpt_tr`应该可以设置成在 GoldenDict 里使用。用的是令牌,即是说免费的。- G1 N" D. S2 r8 h
    0 h1 X  @, Y. k& F
    chatgpt_tr主要是给命令行翻译用的,但也可以自定义提示Prompt以及用于对话。详细用法参看 ``chatgpt_tr.exe --help``。
    % E. ^6 f) o( m) T& E" I7 G: m) Z# G$ v* W# g$ Z6 }, G
    用法
    . b: \% o3 p1 I" A8 D! D$ E4 N- x3 t% A' y
    • 解压
    • 获取access token$ ], ^* F5 a$ U+ S$ K( F$ D
      • 登录 chat.openai.com
      • 访问 https://chat.openai.com/api/auth/session
      • 拷 accessToken备用
      • 此操作只需一次。(access token 有效期约14-30天,但内地ip可能需双向提子因为openai和大伙墙的双重封杀,港澳ip则需找未被openai封杀的带理)
        ( `" F2 n' X$ {7 p7 r( J  d
    • 命令行下设置环境变量OPENAI_ACCESS_TOKEN,例如cmd下 set OPENAI_ACCESS_TOKEN=eyJhbGc... 或用包内的 chatgpt-tr.bat 或sysdm.cpl设置全局环境变量OPENAI_ACCESS_TOKEN
    • 命令行下,例如WIN cmd回车(或开Windows Terminal或cmder或conemu)
      5 V) |7 h! [5 Q& m! n, L
      • chatgpt_tr --help # 帮助
      • chatgpr_tr # 翻译系统剪贴板内容
      • chatgpr_tr this is a test # 翻译 this is a test
        + `* j$ }: N5 ^0 P! F
      $ Q+ n* Y3 k; u; S# D/ E
    9 G$ I& I, O7 ]8 _; ^, e
    可以利用 bat 自定义提示Prompt各种使用场景(例如`chatgpt_tr.exe -p "列出5个中文同义词"  make sense` )。该工具的python源码核心部分只有十几行。网友有兴趣的话我可以提供python源码。, Z& P1 t; z8 b& Q+ K, g4 N
    , v% R$ H! k" @9 c& E, z+ r% S
    该工具基于某个第三方反向代理,代理挂了或被关闭当然工具也就不能用了。 (实测2023-05-05可用)
    3 p' M7 u: i" J# O, G+ |. M
    $ D2 H; K/ {; w1 }' }- c# e典型输出:
    + d& O+ {; E2 @) m, |3 J' g
    # chatgpt_tr amazon code whisperer
    : U: P% |& u. L2 {# [amazon code whisperer( [) t& _" y0 e2 m. c

    / N9 Z6 T7 L8 o7 k* {9 W4 L7 a% t; P版本1:
    : O! m# r6 h0 r- D! |/ i亚马逊代码耳语师
    6 w! i  l9 y/ Q/ b6 a2 p( ~9 J' Y5 T# k. ^1 H) k, ^) Q- p
    版本2:: N' H) M; B5 S7 j. ]& Z" _7 A- o
    亚马逊代码密语者, b7 p+ t0 e$ r" M2 z9 B. R

    , `# v+ A: w8 ?4 I! Z# F版本3:
    2 F0 N) D: j" S8 P1 n9 B亚马逊代码秘语师; ]' u: A# N6 ~' h" v: C; K
    * j3 @) b6 y+ i: w5 P: A
    # chatgpt_tr.exe -p "列出5个中文同义词" make sense. R" j5 z' h& ~; w; U1 ~; y- u
    make sense3 j, o8 j4 r5 v" V" g7 }0 A7 f

    / F% J+ @. B$ T, Z+ |1. 有道理
    9 E9 `) ?  X0 B& K2. 讲得通5 T  {9 E$ y% @/ F4 T7 r
    3. 合理# w% K& \! h' c8 q+ t8 _
    4. 明智) C8 z' L8 J2 }) P! C5 t1 S
    5. 理性
    , S2 s4 A& p/ k) A  J/ V. A- w  X1 O, `5 {
    # chatgpt_tr.exe --help- S( f- _& y' W. T( g2 ]

    8 b  |, e) T/ o Usage: chatgpt_tr.exe [OPTIONS] [TEXT]...8 {+ s! z( p0 \& v% U

    * M/ h+ i2 _% K+ W+ X1 }" d Translate with chatgpt using access token.
    * V, g) u6 C5 L( R6 y. G( i3 N& n6 C set LOGURU_LEVEL=DEBUG or set LOGLEVEL=10 to turn on debug/verbose mode.8 f- }) F( Z5 O0 ]7 |7 _+ t

      `9 [5 g9 e' E# ^( M0 q┌─ Arguments ──────────────────────────────────────────────────────────────────┐
    2 d2 D# b$ ~" N+ L* _  S│   text      [TEXT]...  Source text. [default: None]                          │  `+ F$ V7 @; j, _  _: {3 ]* F* V
    └──────────────────────────────────────────────────────────────────────────────┘
    ) O1 O; M' C( z# o: _- A. _┌─ Options ────────────────────────────────────────────────────────────────────┐8 V7 p" U( m3 D8 I. g
    │ --version  -v,-V               Show version info and a list of supported     │
    . f! }' q$ R3 _- f│                                languages and exit.                           │
    . x; }" ?; q& {│ --clipb    -c                  Use clipboard content if set or if `text` is  │. g- X4 \  B& C. d
    │                                empty.                                        │: O: }7 S6 H* j% w& V2 V" Q3 e
    │ --to-lang  -t         TEXT     Target language when using the default        │
    & d6 F0 Y" [/ W; _6 j0 \8 B& K│                                prompt.                                       │
    % S7 ]( j% B  h│                                [default: 中文]                               │
    $ I2 M6 P+ ~% B│ --numb     -n         INTEGER  number of translation variants when using the │! r4 }6 ^- {3 o, M7 {: p" r% e' E
    │                                default prompt.                               │
    9 b5 S( W/ {- B: l/ I1 ~│                                [default: 3]                                  │3 F; h, E/ l: {& u
    │ --prompt   -p         TEXT     User defined prompt. [default:                │4 [9 E* ]* f8 y0 \; Y7 d0 w
    │                                '将以下内容内容翻成 to_lang                   │$ M+ K, D" M$ Q% T; v5 C
    │                                (中文),不要自由发挥,列出 numb (3) 个版本:  │
    4 _! M0 s5 i! k* x$ M│                                \n']                                          │
    / r# j* K" `. O4 {/ }- \│ --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这个组合算拉郎配
    1 }; P, J+ ?5 b# |1 ]  ?- E4 `, t( `& r/ d6 `% i
    别的不说,就看下图吧:
    ' o( K' r+ b) E  A$ ]" }' [) P% B4 z; J) I) ~% K  [) u% A
    4 x4 J$ ]; i" j& t  f" b/ x0 }+ m
    8 E* t. g; K" o  e
    Define指令不支持中文 用中文吧,它给的解释还是错的
    5 v$ f: N: ?+ s7 {
    + Z- Q0 U7 ?& M5 v3 p- @# e  g0 Y9 t
    「拉郎配」这个词,《现代汉语词典》就有啊:' m& C6 E% _2 W& i$ S# n" H

    $ v+ u& w) l2 S+ L' f; D
    拉郎配- e& g/ H5 j8 p
    lā láng pèi5 r  C/ L3 E& @* A, G
    封建时代为逃避家中未婚女子被选入宫中,仓促找男子跟她成亲。比喻不顾实际,强行使双方联合或合并。

    本帖子中包含更多资源

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

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

    [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:312 |( X& i3 ?% d
    我还是觉得GoldenDict + ChatGPT这个组合算拉郎配
    0 S! \  Y! K5 ?- l! `0 \
      Q# d1 c0 F( R4 T* A3 s% T. \别的不说,就看下图吧:
    拉郎配
    0 C2 U0 s8 L& \9 K/ S7 Z  Z

      p4 f# \( U6 p恰到好处
    * ^; Z1 P; A8 Y9 Q! w- l$ |  u" {& O- H/ w% |' N$ @
    说明问题
  • TA的每日心情
    无聊
    2022-7-11 10:17
  • 签到天数: 97 天

    [LV.6]常住居民II

    发表于 2023-7-24 10:12:50 | 显示全部楼层
    照着弄完报错啊? 麻烦给看看吧. `- j. t, L) e; B+ E% n% X
    查询错误: 程序返回,退出代码为 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-4-27 22:32 , Processed in 0.085632 second(s), 11 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2023, Tencent Cloud.

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