Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

package.json changes #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ jobs:
- name: Install Dependencies
run: npm ci

- name: Build
- name: Typecheck and Build
run: npm run build
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs",
"version": "1.0.0",
"name": "@replit/ai",
"version": "0.0.1",
"description": "",
"scripts": {
"test": "vitest",
Expand All @@ -23,7 +23,8 @@
"keywords": [],
"author": "",
"type": "module",
"license": "ISC",
"private": true,
"license": "UNLICENSED",
"devDependencies": {
"@jest/globals": "^29.6.4",
"@types/node": "^18.0.6",
Expand Down
47 changes: 25 additions & 22 deletions src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import all from 'it-all';
import * as result from './result';
import responseBodyToIterator from './responseBodyToIterator';
import all from "it-all";
import * as result from "./result";
import responseBodyToIterator from "./responseBodyToIterator";

const endpoint = 'http://staging-modelfarm.ai.gcp.replit.com';
const endpoint = "http://staging-modelfarm.ai.gcp.replit.com";
const nonStreamingUrl = `${endpoint}/chat`;
const streamingUrl = `${endpoint}/chat_streaming`;

Expand All @@ -12,7 +12,7 @@ interface Message {
}

// TODO: Add more?
type Model = 'chat-bison';
type Model = "chat-bison";

interface Input {
model: Model;
Expand Down Expand Up @@ -45,19 +45,19 @@ type StreamingChatResponse = result.Result<AsyncGenerator<Message>, ChatError>;
type NonStreamingChatResponse = result.Result<Message, ChatError>;

export default async function chat(
input: NonStreamingInput,
input: NonStreamingInput
): Promise<NonStreamingChatResponse>;
export default async function chat(
input: StreamingInput,
input: StreamingInput
): Promise<StreamingChatResponse>;
export default async function chat(
input: Input,
input: Input
): Promise<StreamingChatResponse | NonStreamingChatResponse> {
let response = await fetch(input.streaming ? streamingUrl : nonStreamingUrl, {
method: 'POST',
method: "POST",
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer $AUTH_TOKEN', // TODO auth
"Content-Type": "application/json",
Authorization: "Bearer $AUTH_TOKEN", // TODO auth
},
body: JSON.stringify({
model: input.model,
Expand All @@ -66,10 +66,10 @@ export default async function chat(
{
context: input.context,
examples: input.examples,
messages: input.messages
}
]
}
messages: input.messages,
},
],
},
}),
});

Expand All @@ -82,15 +82,18 @@ export default async function chat(

if (!response.body) {
return result.Err({
message: 'No response body',
message: "No response body",
statusCode: response.status,
});
}

const iterator = responseBodyToIterator(response.body, (json: any): Message => ({
content: json.responses[0].candidates[0].message.content,
author: json.responses[0].candidates[0].message.author,
}));
const iterator = responseBodyToIterator(
response.body,
(json: any): Message => ({
content: json.responses[0].candidates[0].message.content,
author: json.responses[0].candidates[0].message.author,
})
);

if (input.streaming) {
return result.Ok(iterator);
Expand All @@ -100,11 +103,11 @@ export default async function chat(

let author = allMessages[0]?.author;
if (!author) {
author = "assistant"
author = "assistant";
}

return result.Ok({
content: allMessages.reduce((acc, { content }) => acc + content, ''),
content: allMessages.reduce((acc, { content }) => acc + content, ""),
author,
});
}
Loading