ctod 0.6.3 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,89 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/triple-slash-reference
2
- /// <reference path="../lib/shims.d.ts" />
3
- import { ChatBroker, OpenAI, plugins } from '../lib/index'
4
-
5
- /**
6
- * @test npx ts-node ./examples/chat-demo.ts
7
- */
8
-
9
- const API_KEY = ''
10
- const broker = new ChatBroker({
11
- input: yup => {
12
- return {
13
- indexs: yup.array(yup.string().required()).required(),
14
- question: yup.string().required()
15
- }
16
- },
17
- output: yup => {
18
- const item = yup.object({
19
- name: yup.string().required().meta({
20
- jsonSchema: {
21
- description: '索引名稱'
22
- }
23
- }),
24
- score: yup.number().required().meta({
25
- jsonSchema: {
26
- description: '評比分數'
27
- }
28
- })
29
- }).required()
30
- return {
31
- indexs: yup.array(item).required().meta({
32
- jsonSchema: {
33
- description: '由高到低排序的索引'
34
- }
35
- })
36
- }
37
- },
38
- plugins: {
39
- log: plugins.PrintLogPlugin.use({
40
- detail: true
41
- })
42
- },
43
- install({ attach }) {
44
- attach('start', async({ setPreMessages }) => {
45
- setPreMessages([
46
- {
47
- role: 'system',
48
- content: '你現在是一位擅長分類索引的藥師'
49
- }
50
- ])
51
- })
52
- },
53
- request: OpenAI.createChatRequestWithJsonSchema({
54
- apiKey: API_KEY,
55
- config: {
56
- model: 'gpt-4o-mini'
57
- }
58
- }),
59
- question: async({ indexs, question }) => {
60
- return [
61
- '我有以下索引',
62
- `${JSON.stringify(indexs)}`,
63
- `請幫我解析"${question}"可能是哪個索引`,
64
- '且相關性由高到低排序並給予分數,分數由 0 ~ 1'
65
- ]
66
- }
67
- })
68
-
69
- broker.request({
70
- indexs: ['胃痛', '腰痛', '頭痛', '喉嚨痛', '四肢疼痛'],
71
- question: '喝咖啡,吃甜食,胃食道逆流'
72
- }).then(e => {
73
- console.log('輸出結果:', e.indexs)
74
- /*
75
- [
76
- {
77
- name: '胃痛',
78
- score: 1
79
- },
80
- {
81
- name: '喉嚨痛',
82
- score: 0.7
83
- },
84
- ...
85
- ]
86
- */
87
- }).catch(error => {
88
- console.error('Error:', error?.response?.data?.error?.message ?? error)
89
- })
@@ -1,33 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/triple-slash-reference
2
- /// <reference path="../lib/shims.d.ts" />
3
-
4
- import { Llama3Cpp } from '../lib/index'
5
- import { flow } from 'power-helper'
6
-
7
- flow.run(async() => {
8
- const llama3Cpp = new Llama3Cpp()
9
- const completion = llama3Cpp.createCompletion()
10
- completion.setConfig({
11
- baseUrl: 'http://localhost:12333'
12
- })
13
- completion.talkStream({
14
- messages: [
15
- {
16
- role: 'user',
17
- content: '你是誰'
18
- }
19
- ],
20
- onEnd: () => {
21
- console.log('Is Done!')
22
- },
23
- onError: (error) => {
24
- console.log('Error', error)
25
- },
26
- onWarn: (warn) => {
27
- console.log('Warn', warn)
28
- },
29
- onMessage: (e) => {
30
- console.log(e.message)
31
- }
32
- })
33
- })
Binary file
Binary file
@@ -1,168 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/triple-slash-reference
2
- /// <reference path="../lib/shims.d.ts" />
3
-
4
- import fs from 'fs'
5
- import Yup from 'yup'
6
- import { flow } from 'power-helper'
7
- import { prompt } from 'inquirer'
8
- import { OpenAI, ChatBroker, plugins, templates } from '../lib'
9
-
10
- /**
11
- * @invoke npx ts-node ./examples/vision-demo.ts
12
- */
13
-
14
- const API_KEY = ''
15
- const ImageUrl = 'data:image/png;base64,' + fs.readFileSync('./examples/vision-demo.png').toString('base64')
16
- const ImageExUrl = 'data:image/png;base64,' + fs.readFileSync('./examples/vision-demo-ex.png').toString('base64')
17
-
18
- flow.run(async () => {
19
- const drinkRule = (yup: typeof Yup) => {
20
- return yup.object({
21
- name: yup.string().required(),
22
- prices: yup.object({
23
- M: yup.number().nullable(),
24
- L: yup.number().nullable()
25
- })
26
- })
27
- }
28
- const broker = new ChatBroker({
29
- input: yup => {
30
- return {
31
- exists: yup.array(drinkRule(yup)).required()
32
- }
33
- },
34
- output: yup => {
35
- return {
36
- drinks: yup.array(drinkRule(yup)).required()
37
- }
38
- },
39
- plugins: {
40
- print: plugins.PrintLogPlugin.use({
41
- detail: false
42
- })
43
- },
44
- install: () => {
45
- return null
46
- },
47
- request: async(messages) => {
48
- const openai = new OpenAI(API_KEY)
49
- const vision = openai.createVision()
50
- vision.setConfig({
51
- maxTokens: 4096
52
- })
53
- const result = await vision.view([
54
- {
55
- role: 'system',
56
- content: '你現在是一個分析菜單的助手,你擅長從圖片中找到商品名稱與價格並歸納'
57
- },
58
- {
59
- role: 'user',
60
- content: [
61
- {
62
- type: 'text',
63
- text: '以下是菜單的一部分,我主要需要藍色文字的商品部分,這部分你可以解讀出什麼?'
64
- },
65
- {
66
- type: 'image_url',
67
- image_url: {
68
- url: ImageExUrl
69
- }
70
- }
71
- ]
72
- },
73
- {
74
- role: 'assistant',
75
- content: '我從圖片中知道熟成紅茶 中杯25元 大杯30元'
76
- },
77
- {
78
- role: 'user',
79
- content: [
80
- {
81
- type: 'text',
82
- text: messages[0].content
83
- },
84
- {
85
- type: 'image_url',
86
- image_url: {
87
- url: ImageUrl
88
- }
89
- }
90
- ]
91
- }
92
- ])
93
- return result.text
94
- },
95
- question: async ({ exists }) => {
96
- const ex = exists.map(e => {
97
- const prices = Object
98
- .entries(e.prices)
99
- .filter(e => e[1])
100
- .map(([key, value]) => `${key}:${value}`)
101
- return `* ${e.name}: ${prices.join('/')}`
102
- })
103
- return templates.requireJsonResponse([
104
- '你的解讀完全正確,現在請幫我條列出菜單上所有商品,這個菜單中已知以下資料,你解讀的結果如果沒有以下資料代表你的解讀不對:',
105
- '"""',
106
- ...ex,
107
- '"""'
108
- ], {
109
- drinks: {
110
- desc: '商品清單,不要回傳已知商品',
111
- example: [
112
- {
113
- name: '熟成紅茶',
114
- prices: {
115
- M: 25,
116
- L: 30
117
- }
118
- }
119
- ]
120
- }
121
- })
122
- }
123
- })
124
- try {
125
- const exists = [
126
- {
127
- name: '熟成紅茶',
128
- prices: {
129
- M: 25,
130
- L: 30
131
- }
132
- }
133
- ] as any[]
134
- // eslint-disable-next-line no-constant-condition
135
- while (true) {
136
- const result = await broker.request({
137
- exists
138
- })
139
- const keys: string[] = exists.map(e => e.name)
140
- const { drinks } = await prompt([
141
- {
142
- type: 'checkbox',
143
- name: 'drinks',
144
- message: '請選擇正確的飲料',
145
- choices: result.drinks.filter(e => !keys.includes(e.name)).map(e => {
146
- return {
147
- name: `${e.name} ${Object.entries(e.prices).map(([key, value]) => `${key}:${value}`).join(' ')}`,
148
- value: e
149
- }
150
- })
151
- },
152
- ])
153
- exists.push(...drinks)
154
- const { keep } = await prompt([
155
- {
156
- type: 'confirm',
157
- name: 'keep',
158
- message: '是否繼續?'
159
- }
160
- ])
161
- if (keep === false) {
162
- break
163
- }
164
- }
165
- } catch (error: any) {
166
- console.error('Error:', error?.response?.data?.error?.message ?? error)
167
- }
168
- })