Skip to content

Commit

Permalink
feat: migrate groq to deepseek (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Jan 20, 2025
1 parent 4b205ad commit 8af4c7f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Fānyì (翻译)

A 🇨🇳 and 🇺🇸🇬🇧 translator in your command line, powered by iciba and groq.
A 🇨🇳 and 🇺🇸🇬🇧 translator in your command line, powered by iciba and deepseek.

[![NPM version][npm-image]][npm-url]
[![npm download][download-image]][download-url]
Expand All @@ -22,7 +22,7 @@ A 🇨🇳 and 🇺🇸🇬🇧 translator in your command line, powered by icib

</div>

[fanyi@9.0.0](https://github.com/afc163/fanyi/releases/tag/v9.0.0) 正式发布!这一版对原有功能进行了大幅裁剪,移除了速度慢和失效的翻译源,以及对 say 命令的依赖,并引入 Groq 加持的 llama3 进行翻译,翻译速度一流。代码也做了整体重构,依旧是你命令行中**最简单顺手快捷**的中英文翻译工具。
[fanyi@9.0.0](https://github.com/afc163/fanyi/releases/tag/v9.0.0) 正式发布!这一版对原有功能进行了大幅裁剪,移除了速度慢和失效的翻译源,以及对 say 命令的依赖,并引入 deepseek 加持的 llama3 进行翻译,翻译速度一流。代码也做了整体重构,依旧是你命令行中**最简单顺手快捷**的中英文翻译工具。

- 🐑 增加 llama3 翻译结果。
- 🌈 渐变色彩输出,更加灵动浮夸。
Expand Down Expand Up @@ -57,7 +57,7 @@ For short:
$ fy word
```

Translation data is fetched from [iciba.com](https://iciba.com) and grop ai, and only support translation between Chinese and English.
Translation data is fetched from [iciba.com](https://iciba.com) and deepseek ai, and only support translation between Chinese and English.

Translate one word.

Expand Down Expand Up @@ -138,7 +138,7 @@ Example:
```bash
$ fanyi config list // list all configuration options
$ fanyi config set iciba false // disable iciba globally
$ fanyi config set groq false // disable groq globally
$ fanyi config set deepseek false // disable deepseek globally
$ fanyi config set color false // disable color globally
$ fanyi config set GROQ_API_KEY your-api-key // set GROQ_API_KEY
$ fanyi config set LLM_API_KEY your-api-key // set LLM_API_KEY
```
6 changes: 3 additions & 3 deletions bin/fanyi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ program
.argument('<value>', '配置项值')
.action(async (key, value) => {
const options = {};
if (key === 'GROQ_API_KEY') {
if (key === 'LLM_API_KEY') {
options[key] = value;
} else {
options[key] = value === 'true' ? true : value === 'false' ? false : value;
Expand All @@ -69,8 +69,8 @@ program.on('--help', () => {
console.log(`${chalk.cyan(' $ ')}fanyi chinglish`);
console.log(`${chalk.cyan(' $ ')}fanyi config set color true`);
console.log(`${chalk.cyan(' $ ')}fanyi config set iciba true`);
console.log(`${chalk.cyan(' $ ')}fanyi config set groq true`);
console.log(`${chalk.cyan(' $ ')}fanyi config set GROQ_API_KEY your_api_key_here`);
console.log(`${chalk.cyan(' $ ')}fanyi config set deepseek true`);
console.log(`${chalk.cyan(' $ ')}fanyi config set LLM_API_KEY your_api_key_here`);
console.log(`${chalk.cyan(' $ ')}fanyi config list`);
console.log('');
});
Expand Down
Binary file modified bun.lockb
Binary file not shown.
23 changes: 12 additions & 11 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import gradient from 'gradient-string';
import { Groq } from 'groq-sdk';
import fetch from 'node-fetch';
import OpenAI from 'openai';
import ora from 'ora';
import { printIciba } from './lib/iciba.mjs';

Expand All @@ -23,7 +23,7 @@ const gradients = [

export default async (word, options) => {
console.log('');
const { iciba, groq, GROQ_API_KEY } = options;
const { iciba, deepseek, LLM_API_KEY } = options;
const endcodedWord = encodeURIComponent(word);

// iciba
Expand All @@ -43,15 +43,18 @@ export default async (word, options) => {
}
}

// groq ai
if (isTrueOrUndefined(groq)) {
const groqClient = new Groq({
apiKey: GROQ_API_KEY || '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',
// deepseek
if (isTrueOrUndefined(deepseek)) {
const openai = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: LLM_API_KEY || 'sk-a6325c2f3d2044968e6a83f249cc1541',
});
const model = 'llama-3.1-70b-versatile';

const model = 'deepseek-chat';

const spinner = ora(`正在请教 ${model}...`).start();
try {
const chatCompletion = await groqClient.chat.completions.create({
const chatCompletion = await openai.chat.completions.create({
messages: [
{
role: 'system',
Expand Down Expand Up @@ -107,9 +110,7 @@ export default async (word, options) => {
},
],
model,
temperature: 0.3,
max_tokens: 1024,
top_p: 0.8,
temperature: 1.3,
});
spinner.stop();
const randomGradient = gradients[Math.floor(Math.random() * gradients.length)];
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"fy": "bin/fanyi.mjs",
"fanyi": "bin/fanyi.mjs"
},
"keywords": ["chinese", "translator", "iciba", "groq", "llama", "cli", "fanyi"],
"keywords": ["chinese", "translator", "iciba", "deepseek", "cli", "fanyi"],
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -25,8 +25,8 @@
"dayjs": "^1.11.13",
"fast-xml-parser": "^4.5.0",
"gradient-string": "^3.0.0",
"groq-sdk": "^0.9.0",
"node-fetch": "^3.3.2",
"openai": "^4.79.1",
"ora": "^8.1.0",
"update-notifier": "^7.3.1"
},
Expand Down
8 changes: 4 additions & 4 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Commands:
$ fanyi chinglish
$ fanyi config set color true
$ fanyi config set iciba true
$ fanyi config set groq true
$ fanyi config set GROQ_API_KEY your_api_key_here
$ fanyi config set deepseek true
$ fanyi config set LLM_API_KEY your_api_key_here
$ fanyi config list
"
Expand All @@ -45,8 +45,8 @@ Commands:
$ fanyi chinglish
$ fanyi config set color true
$ fanyi config set iciba true
$ fanyi config set groq true
$ fanyi config set GROQ_API_KEY your_api_key_here
$ fanyi config set deepseek true
$ fanyi config set LLM_API_KEY your_api_key_here
$ fanyi config list
"
Expand Down

0 comments on commit 8af4c7f

Please sign in to comment.