@fencyai/react 0.1.24 → 0.1.26
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.
- package/lib/hooks/useChatCompletion.js +19 -7
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -1
- package/package.json +4 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// hooks/useChatCompletion.ts
|
|
2
|
-
import { createChatCompletion, createChatCompletionStream } from '@fencyai/js';
|
|
2
|
+
import { createChatCompletion, createChatCompletionStream, } from '@fencyai/js';
|
|
3
3
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
4
4
|
import z from 'zod';
|
|
5
5
|
import { useEventSource } from './useEventSource';
|
|
@@ -11,11 +11,15 @@ export function useChatCompletion() {
|
|
|
11
11
|
const [stream, setStream] = useState(null);
|
|
12
12
|
const createStreamingChatCompletion = useCallback(async (params) => {
|
|
13
13
|
// Step 1: Create stream if not exists
|
|
14
|
-
const s = await createChatCompletionStream({
|
|
14
|
+
const s = await createChatCompletionStream({
|
|
15
|
+
pk: fency.fency.publishableKey,
|
|
16
|
+
baseUrl: fency.fency.baseUrl,
|
|
17
|
+
});
|
|
15
18
|
setStream(s);
|
|
16
19
|
// Step 2: Send chat completion
|
|
17
20
|
const chatCompletion = await createChatCompletion({
|
|
18
21
|
pk: fency.fency.publishableKey,
|
|
22
|
+
baseUrl: fency.fency.baseUrl,
|
|
19
23
|
request: {
|
|
20
24
|
chatCompletionStreamId: s.chatCompletionStreamId,
|
|
21
25
|
openai: {
|
|
@@ -31,23 +35,30 @@ export function useChatCompletion() {
|
|
|
31
35
|
};
|
|
32
36
|
}, [fency]);
|
|
33
37
|
const createSynchronousChatCompletion = useCallback(async (params) => {
|
|
34
|
-
const jsonSchema = params.responseFormat
|
|
38
|
+
const jsonSchema = params.responseFormat
|
|
39
|
+
? z.toJSONSchema(params.responseFormat)
|
|
40
|
+
: undefined;
|
|
35
41
|
console.log(jsonSchema);
|
|
36
42
|
const chatCompletion = await createChatCompletion({
|
|
37
43
|
pk: fency.fency.publishableKey,
|
|
44
|
+
baseUrl: fency.fency.baseUrl,
|
|
38
45
|
request: {
|
|
39
46
|
openai: {
|
|
40
47
|
model: params.model,
|
|
41
|
-
responseJsonSchema: jsonSchema
|
|
48
|
+
responseJsonSchema: jsonSchema
|
|
49
|
+
? JSON.stringify(jsonSchema)
|
|
50
|
+
: undefined,
|
|
42
51
|
messages: [{ role: 'user', content: params.prompt }],
|
|
43
52
|
},
|
|
44
53
|
},
|
|
45
54
|
});
|
|
46
55
|
setChatCompletions((prev) => [...prev, chatCompletion]);
|
|
47
|
-
if (chatCompletion.responseIsStructured &&
|
|
56
|
+
if (chatCompletion.responseIsStructured &&
|
|
57
|
+
params.responseFormat &&
|
|
58
|
+
chatCompletion.response) {
|
|
48
59
|
return {
|
|
49
60
|
...chatCompletion,
|
|
50
|
-
structuredResponse: params.responseFormat.parse(JSON.parse(chatCompletion.response))
|
|
61
|
+
structuredResponse: params.responseFormat.parse(JSON.parse(chatCompletion.response)),
|
|
51
62
|
};
|
|
52
63
|
}
|
|
53
64
|
return chatCompletion;
|
|
@@ -56,7 +67,8 @@ export function useChatCompletion() {
|
|
|
56
67
|
const completions = [];
|
|
57
68
|
for (const chatCompletion of chatCompletions) {
|
|
58
69
|
const relevantChunks = chunks
|
|
59
|
-
.filter((chunk) => chunk.chatCompletionId ===
|
|
70
|
+
.filter((chunk) => chunk.chatCompletionId ===
|
|
71
|
+
chatCompletion.chatCompletionId)
|
|
60
72
|
.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
61
73
|
const fullMessage = relevantChunks
|
|
62
74
|
.map((chunk) => chunk.content)
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { FencyProvider } from './FencyProvider';
|
|
2
2
|
export { useChatCompletion } from './hooks/useChatCompletion';
|
|
3
3
|
export { useFency } from './hooks/useFency';
|
|
4
|
-
export type { FencyContext, FencyOptions, FencyProviderProps } from './FencyProvider';
|
|
4
|
+
export type { FencyContext, FencyOptions, FencyProviderProps, } from './FencyProvider';
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fencyai/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "staklau <steinaageklaussen@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"zod": "^4.0.5"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@fencyai/js": "^0.1.
|
|
38
|
+
"@fencyai/js": "^0.1.26",
|
|
39
39
|
"@types/jest": "^29.5.11",
|
|
40
40
|
"@types/node": "^20.10.5",
|
|
41
41
|
"@types/react": "^18.2.45",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"typescript": "^5.3.3"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"@fencyai/js": "^0.1.
|
|
47
|
+
"@fencyai/js": "^0.1.26",
|
|
48
48
|
"react": ">=16.8.0"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "6459e4ef59624f71aa5fc50208e06c4e18419e6f"
|
|
51
51
|
}
|