Skip to content

Commit

Permalink
feat: support claude-2 and anthropic-version in cloudflare-worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsang4 committed Jul 11, 2023
1 parent 4084f3f commit ba17d37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.venv
__pycache__
__pycache__
node_modules
61 changes: 34 additions & 27 deletions cloudflare-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const version = '0.3.1';
const version = '0.4.0';

addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
Expand Down Expand Up @@ -82,39 +82,48 @@ async function streamJsonResponseBodies(response, writable) {
const decoder = new TextDecoder();

let buffer = '';
let prevCompletion = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
if (buffer) {
writer.write(encoder.encode(buffer));
}
writer.write(encoder.encode('data: [DONE]'));
break;
}
const currentText = decoder.decode(value, { stream: true }); // stream: true is important here,fix the bug of incomplete line
if (currentText.startsWith('event: ping')) {
continue;
}
const sanitizedText = currentText.replace('event: completion', '').trim();
// if meet new line, then write the buffer to the writer
if (currentText.startsWith('data: ')) {
if (buffer) {
if (buffer.startsWith('data: ') && buffer.endsWith('}')) {
try {
const decodedLine = JSON.parse(buffer.slice(5));
const newCompletion = decodedLine['completion'].replace(
prevCompletion,
''
);
const transformedLine = claudeToChatGPTResponse(
{
...decodedLine,
completion: newCompletion,
},
true
);
const completion = decodedLine['completion'];
const stop_reason = decodedLine['stop_reason'];
let transformedLine = {};
if (stop_reason) {
transformedLine = claudeToChatGPTResponse(
{
completion: '',
stop_reason: stop_reason,
},
true
);
} else {
transformedLine = claudeToChatGPTResponse(
{
...decodedLine,
completion: completion,
},
true
);
}
writer.write(
encoder.encode(`data: ${JSON.stringify(transformedLine)}\n\n`)
);
prevCompletion = decodedLine['completion'];
buffer = '';
}
} catch (e) {}
}
buffer += currentText;
buffer += sanitizedText;
}

await writer.close();
Expand Down Expand Up @@ -153,24 +162,22 @@ async function handleRequest(request) {

// OpenAI API 转换为 Claude API
const prompt = convertMessagesToPrompt(messages);
let maxTokensToSample = 100000;
if (model !== 'claude-2') {
maxTokensToSample = 9016;
}
const claudeRequestBody = {
prompt,
model: claudeModel,
temperature,
max_tokens_to_sample: maxTokensToSample,
max_tokens_to_sample: 9016,
stop_sequences: stop,
stream,
};

const claudeResponse = await fetch(`${CLAUDE_BASE_URL}/v1/complete`, {
method: 'POST',
headers: {
accept: 'application/json',
'Content-Type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': '2023-06-01',
},
body: JSON.stringify(claudeRequestBody),
});
Expand Down Expand Up @@ -285,7 +292,7 @@ const models_list = [
parent: null,
},
{
id: 'gpt-4-0314',
id: 'gpt-4-0613',
object: 'model',
created: 1678604601,
owned_by: 'openai',
Expand Down

0 comments on commit ba17d37

Please sign in to comment.