Skip to content

Commit

Permalink
feat: try openai
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Jun 25, 2024
1 parent 61fa2e0 commit 68fb02b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node-terminal",
"name": "开发模式",
"request": "launch",
"command": "pnpm run dev",
"cwd": "${workspaceFolder}"
}
]
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"explorer.fileNesting.patterns": {
".gitignore": ".gitconfig",
"wxt.config.ts": "auto-imports*.ts, components*.ts, tailwind.config.cjs, tsconfig.json, types.d.ts, postcss.config.cjs"
}
},
"editor.formatOnSave": true
}
44 changes: 35 additions & 9 deletions entrypoints/sidepanel/Sidepanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { html2md, md2html } from '@/utils/parser';
import { ElMessage as notify } from 'element-plus';
import { checkStatus } from '@/utils/checkStatus';
import { isCheckTw5Storage, tagStorage, portStorage } from '@/utils/storage';
import getAI from '@/utils/openai';
const editRef = ref<HTMLInputElement>();
const isChecking = ref(false);
Expand Down Expand Up @@ -71,6 +72,7 @@ async function getContent(
onMounted(() => {
getContent();
// getAI();
});
const handleSave = () =>
Expand Down Expand Up @@ -276,25 +278,38 @@ const toggleInfoDialog = () => {
</div>

<div>
<ElTabs type="" :model-value="currentTab" class="">
<ElTabs
type=""
:model-value="currentTab"
class="">
<!-- preview -->
<ElTabPane name="preview" class="overflow-y-auto h-screen">
<ElTabPane
name="preview"
class="overflow-y-auto h-screen">
<template #label>
<WI.FaFileTextO /> <span class="ml-1">预览</span>
</template>
<div v-if="title">
<div class="flex items-center justify-center gap-2">
<h2>
<a :href="link" target="_blank" v-if="link">
<img alt="" :src="faviconUrl" class="rounded-full size-4" />
<a
:href="link"
target="_blank"
v-if="link">
<img
alt=""
:src="faviconUrl"
class="rounded-full size-4" />
</a>
{{ title }}
</h2>
</div>
<!-- <el-divider border-style="dashed" /> -->
<article
class="prose prose-gray max-w-none prose-sm dark:prose-invert flex-wrap prose-img:max-w-[300px] prose-img:my-0 prose-img:rounded-md prose-video:max-w-[300px] prose-video:max-h-[300px] prose-video:my-0">
<div v-html="html" class="mx-2"></div>
<div
v-html="html"
class="mx-2"></div>
</article>
</div>
</ElTabPane>
Expand All @@ -306,7 +321,10 @@ const toggleInfoDialog = () => {
<span class="ml-1">编辑</span>
</template>

<ElInput type="text" v-model="title" class="mb-4" />
<ElInput
type="text"
v-model="title"
class="mb-4" />

<ElInput
ref="editRef"
Expand Down Expand Up @@ -341,7 +359,10 @@ const toggleInfoDialog = () => {
<h2>Nodejs TiddlyWiki5 端口</h2>
<div class="flex gap-2">
<ElInput v-model.trim.number="port" />
<ElButton type="success" plain @click="savePort(port!)"
<ElButton
type="success"
plain
@click="savePort(port!)"
>保存</ElButton
>
</div>
Expand Down Expand Up @@ -402,8 +423,13 @@ const toggleInfoDialog = () => {
</div>
</ElTabPane>

<el-drawer v-model="infoDialogStatus" direction="btt" title="连接信息">
<Info :status="status" :json="json" />
<el-drawer
v-model="infoDialogStatus"
direction="btt"
title="连接信息">
<Info
:status="status"
:json="json" />
</el-drawer>
</ElTabs>
</div>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"element-plus": "2.6.2",
"groq-sdk": "^0.3.3",
"ofetch": "^1.3.4",
"openai": "^4.52.0",
"postcss": "^8.4.38",
"rehype-document": "^7.0.3",
"rehype-format": "^5.0.0",
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions utils/openai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import OpenAI from 'openai';

// TODO: 总结一下这篇文章
/** @see: https://docs.siliconflow.cn/reference/chat-completions-1 */
async function getAI(content = 'who you are') {
const openai = new OpenAI({
apiKey: 'sk-xxxxxx',
baseURL: 'https://api.siliconflow.cn/v1',
dangerouslyAllowBrowser: true,
});

const params: OpenAI.Chat.ChatCompletionCreateParams = {
messages: [{ role: 'user', content }],
model: 'Qwen/Qwen2-72B-Instruct',
stream: true,
};
const stream = await openai.chat.completions.create(params);
for await (const chunk of stream) {
// process.stdout.write(chunk.choices[0]?.delta?.content || '');
console.log(chunk.choices[0]?.delta?.content || '');
}
}

export default getAI;

0 comments on commit 68fb02b

Please sign in to comment.