1
- import { getUserConfig } from '../../config/index.mjs'
1
+ import { getUserConfig , Models } from '../../config/index.mjs'
2
2
import { pushRecord , setAbortController } from './shared.mjs'
3
3
import { fetchSSE } from '../../utils/fetch-sse.mjs'
4
4
import { isEmpty } from 'lodash-es'
5
+ import { getConversationPairs } from '../../utils/get-conversation-pairs.mjs'
5
6
6
7
/**
7
8
* @param {Runtime.Port } port
@@ -11,28 +12,28 @@ import { isEmpty } from 'lodash-es'
11
12
export async function generateAnswersWithClaudeApi ( port , question , session ) {
12
13
const { controller, messageListener, disconnectListener } = setAbortController ( port )
13
14
const config = await getUserConfig ( )
15
+ const modelName = session . modelName
14
16
15
- let prompt = ''
16
- for ( const record of session . conversationRecords . slice ( - config . maxConversationContextLength ) ) {
17
- prompt += '\n\nHuman: ' + record . question + '\n\nAssistant: ' + record . answer
18
- }
19
- prompt += `\n\nHuman: ${ question } \n\nAssistant:`
17
+ const prompt = getConversationPairs (
18
+ session . conversationRecords . slice ( - config . maxConversationContextLength ) ,
19
+ false ,
20
+ )
21
+ prompt . push ( { role : 'user' , content : question } )
20
22
21
23
let answer = ''
22
- await fetchSSE ( `https://api.anthropic.com/v1/complete ` , {
24
+ await fetchSSE ( `https://api.anthropic.com/v1/messages ` , {
23
25
method : 'POST' ,
24
26
signal : controller . signal ,
25
27
headers : {
26
28
'Content-Type' : 'application/json' ,
27
- accept : 'application/json' ,
28
29
'anthropic-version' : '2023-06-01' ,
29
30
'x-api-key' : config . claudeApiKey ,
30
31
} ,
31
32
body : JSON . stringify ( {
32
- model : 'claude-2' ,
33
- prompt : prompt ,
33
+ model : Models [ modelName ] . value ,
34
+ messages : prompt ,
34
35
stream : true ,
35
- max_tokens_to_sample : config . maxResponseTokenLength ,
36
+ max_tokens : config . maxResponseTokenLength ,
36
37
temperature : config . temperature ,
37
38
} ) ,
38
39
onMessage ( message ) {
@@ -45,22 +46,17 @@ export async function generateAnswersWithClaudeApi(port, question, session) {
45
46
console . debug ( 'json error' , error )
46
47
return
47
48
}
48
-
49
- // The Claude v2 API may send metadata fields, handle them here
50
- if ( data . conversationId ) session . conversationId = data . conversationId
51
- if ( data . parentMessageId ) session . parentMessageId = data . parentMessageId
52
-
53
- // In Claude's case, the "completion" key holds the text
54
- if ( data . completion ) {
55
- answer += data . completion
56
- port . postMessage ( { answer : answer , done : false , session : null } )
57
- }
58
-
59
- // Check if the message indicates that Claude is done
60
- if ( data . stop_reason === 'stop_sequence' ) {
49
+ if ( data ?. type === 'message_stop' ) {
61
50
pushRecord ( session , question , answer )
62
51
console . debug ( 'conversation history' , { content : session . conversationRecords } )
63
52
port . postMessage ( { answer : null , done : true , session : session } )
53
+ return
54
+ }
55
+
56
+ const delta = data ?. delta ?. text
57
+ if ( delta ) {
58
+ answer += delta
59
+ port . postMessage ( { answer : answer , done : false , session : null } )
64
60
}
65
61
} ,
66
62
async onStart ( ) { } ,
0 commit comments