Skip to content

Commit bdfdf23

Browse files
committed
feat(puterai): add togetherai
1 parent 7b254e8 commit bdfdf23

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

package-lock.json

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/backend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"string-length": "^6.0.0",
7070
"svgo": "^3.0.2",
7171
"tiktoken": "^1.0.15",
72+
"together-ai": "^0.6.0-alpha.4",
7273
"tweetnacl": "^1.0.3",
7374
"ua-parser-js": "^1.0.38",
7475
"uglify-js": "^3.17.4",

src/backend/src/modules/puterai/PuterAIModule.js

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class PuterAIModule extends AdvancedBase {
3333
const { ClaudeService } = require('./ClaudeService');
3434
services.registerService('claude', ClaudeService);
3535
}
36+
37+
if ( !! config?.services?.['together-ai'] ) {
38+
const { TogetherAIService } = require('./TogetherAIService');
39+
services.registerService('together-ai', TogetherAIService);
40+
}
3641
}
3742
}
3843

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const { PassThrough } = require("stream");
2+
const BaseService = require("../../services/BaseService");
3+
const { TypedValue } = require("../../services/drivers/meta/Runtime");
4+
const { nou } = require("../../util/langutil");
5+
6+
class TogetherAIService extends BaseService {
7+
static MODULES = {
8+
['together-ai']: require('together-ai'),
9+
kv: globalThis.kv,
10+
uuidv4: require('uuid').v4,
11+
}
12+
13+
async _init () {
14+
const require = this.require;
15+
const Together = require('together-ai');
16+
this.together = new Together({
17+
apiKey: this.config.apiKey
18+
});
19+
this.kvkey = this.modules.uuidv4();
20+
debugger;
21+
}
22+
23+
static IMPLEMENTS = {
24+
['puter-chat-completion']: {
25+
async complete ({ messages, stream, model }) {
26+
console.log('model?', model);
27+
const completion = await this.together.chat.completions.create({
28+
model: model ??
29+
'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
30+
messages: messages,
31+
stream,
32+
});
33+
34+
if ( stream ) {
35+
const stream = new PassThrough();
36+
const retval = new TypedValue({
37+
$: 'stream',
38+
content_type: 'application/x-ndjson',
39+
chunked: true,
40+
}, stream);
41+
(async () => {
42+
for await ( const chunk of completion ) {
43+
console.log('IT IS THIS STRING', chunk);
44+
if ( chunk.choices.length < 1 ) continue;
45+
if ( chunk.choices[0].finish_reason ) {
46+
stream.end();
47+
break;
48+
}
49+
if ( nou(chunk.choices[0].delta.content) ) continue;
50+
const str = JSON.stringify({
51+
text: chunk.choices[0].delta.content
52+
});
53+
stream.write(str + '\n');
54+
}
55+
})();
56+
return retval;
57+
}
58+
59+
return completion.choices[0];
60+
}
61+
}
62+
}
63+
}
64+
65+
module.exports = {
66+
TogetherAIService,
67+
};

0 commit comments

Comments
 (0)