Skip to content

Commit

Permalink
Merge branch 'claude' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ae86jack committed Aug 15, 2024
2 parents 26c2cd0 + bd420e0 commit 1bdcd8c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Tars 是一个 Obsidian 插件,支持 Kimi、豆包、阿里千问、百度千

在设置页面添加一个 AI 助手,设置 API 密钥,然后在编辑器中使用相应的标签来触发 AI 助手。

如果在设置页面的 AI 助手中没有你想要的 model 类型,可以在设置中的“覆盖输入参数”进行配置,输入 JSON 格式,例如 `{"model":"你想要的model"}`
如果在设置页面的 AI 助手中没有你想要的 model 类型,服务器地址需要中转,可以在设置中的“覆盖输入参数”进行配置,输入 JSON 格式,例如 `{"model":"你想要的model", "baseURL": "中转地址"}`

## 对话语法

Expand Down
45 changes: 45 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "C Jack<https://github.com/ae86jack>",
"license": "MIT",
"devDependencies": {
"@anthropic-ai/sdk": "^0.25.1",
"@types/node": "^16.18.101",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
Expand Down
55 changes: 55 additions & 0 deletions src/providers/claude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Anthropic from '@anthropic-ai/sdk'

import { t } from 'src/lang/helper'
import { BaseOptions, Message, SendRequest, Vendor } from '.'

interface ClaudeMessage {
role: 'user' | 'assistant'
content: string
}

const sendRequestFunc = (settings: BaseOptions): SendRequest =>
async function* (rawMessages: Message[]) {
const { parameters, ...optionsExcludingParams } = settings
const options = { ...optionsExcludingParams, ...parameters } // 这样的设计,让parameters 可以覆盖掉前面的设置 optionsExcludingParams
const { apiKey, baseURL, model, ...remains } = options
if (!apiKey) throw new Error(t('API key is required'))

const messages = rawMessages.filter((m) => m.role === 'user' || m.role == 'assistant') as ClaudeMessage[]
const client = new Anthropic({
apiKey,
baseURL
})

const stream = client.messages.stream({
model,
messages,
max_tokens: 1024
// ...remains
})

// for await (const part of stream) {
// const text = part.choices[0]?.delta?.content
// if (!text) continue
// yield text
// }
for await (const event of stream) {
console.log('event', event)
yield 'todo'
}
}

const models = ['claude-3-opus-20240229', 'claude-3-haiku-20240307', 'claude-3-5-sonnet-20240620']

export const claudeVendor: Vendor = {
name: 'Claude',
defaultOptions: {
apiKey: '',
baseURL: 'https://fast.bemore.lol',
model: models[0],
parameters: {}
},
sendRequestFunc,
models,
websiteToObtainKey: ''
}
4 changes: 3 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { openAIVendor } from './providers/openAI'
import { qianFanVendor } from './providers/qianFan'
import { qwenVendor } from './providers/qwen'
import { zhipuVendor } from './providers/zhipu'
import { claudeVendor } from './providers/claude'

export interface PluginSettings {
providers: ProviderSettings[]
Expand All @@ -30,5 +31,6 @@ export const availableVendors: Vendor[] = [
qianFanVendor,
qwenVendor,
zhipuVendor,
deepSeekVendor
deepSeekVendor,
claudeVendor
]

0 comments on commit 1bdcd8c

Please sign in to comment.