Merge pull request #17 from Pierre-Mike/main

Some cleanup of the JSDOC and other
This commit is contained in:
Wlad Paiva
2023-11-10 11:07:13 -03:00
committed by GitHub
10 changed files with 21 additions and 19 deletions
-1
View File
@@ -136,7 +136,6 @@ dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
+1 -2
View File
@@ -25,8 +25,7 @@ export const aibitat = new AIbitat({
'https://raw.githubusercontent.com/wladiston/aibitat/main/README.md',
)
const html = await response.text()
const text = cheerio.load(html).text()
return text
return cheerio.load(html).text()
},
})
.agent(Agent.HUMAN, {
+2 -2
View File
@@ -59,12 +59,12 @@ const aibitat = new AIbitat()
}
if (typeof Bun !== 'undefined') {
Bun.write(filename, code)
await Bun.write(filename, code)
return `File ${filename} created and can be executed with "bun ${filename}"`
}
const fs = await import('fs')
await fs.writeFile(filename, code, () => {})
fs.writeFile(filename, code, () => {})
return `File ${filename} created and can be executed with "node ${filename}"`
},
})
+1 -1
View File
@@ -3,7 +3,7 @@ import OpenAI from 'openai'
import {RetryError} from './error.ts'
import {AIbitat} from './index.ts'
import type {Provider} from './providers/index.ts'
import type {Provider} from './providers'
// HACK: Mock the AI provider.
// This is still needed because Bun doesn't support mocking modules yet.
+8 -7
View File
@@ -64,7 +64,7 @@ export type ChannelConfig<T extends Provider = 'openai'> = ProviderConfig<T> & {
role?: string
/**
* The maximum number of rounds a agent can chat with the channel
* The maximum number of rounds an agent can chat with the channel
* @default 10
*/
maxRounds?: number
@@ -307,7 +307,7 @@ export class AIbitat<T extends Provider> {
/**
* Interruption the chat.
*
* @param chat The nodes that participated in the interruption.
* @param route The nodes that participated in the interruption.
* @returns
*/
private interrupt(route: Route) {
@@ -379,7 +379,8 @@ export class AIbitat<T extends Provider> {
* Register an error in the chat history.
* This will trigger the `onError` event.
*
* @param message
* @param route
* @param error
*/
private newError(route: Route, error: unknown) {
const chat = {
@@ -426,7 +427,7 @@ export class AIbitat<T extends Provider> {
/**
* Recursively chat between two nodes.
*
* @param chat The nodes that are going to participate in the chat.
* @param route
* @param keepAlive Whether to keep the chat alive.
*/
private async chat(route: Route, keepAlive = true) {
@@ -550,7 +551,7 @@ export class AIbitat<T extends Provider> {
node => !this.hasReachedMaximumRounds(channel, node),
)
// remove the last node that chatted with the channel so it doesn't chat again
// remove the last node that chatted with the channel, so it doesn't chat again
const lastChat = this._chats.filter(c => c.to === channel).at(-1)
if (lastChat) {
const index = availableNodes.indexOf(lastChat.from)
@@ -621,8 +622,8 @@ Only return the role.
/**
* Ask the for the AI provider to generate a reply to the chat.
*
* @param chat.to The node that sent the chat.
* @param chat.from The node that will reply to the chat.
* @param route.to The node that sent the chat.
* @param route.from The node that will reply to the chat.
*/
private async reply(route: Route) {
// get the provider for the node that will reply
+1 -2
View File
@@ -23,9 +23,8 @@ function cli({
setup(aibitat) {
let printing: Promise<void>[] = []
aibitat.onError(error => {
aibitat.onError(async error => {
console.error(chalk.red(` error: ${(error as Error).message}`))
if (error instanceof RetryError) {
console.error(chalk.red(` retrying in 60 seconds...`))
setTimeout(() => {
+1
View File
@@ -12,6 +12,7 @@ import type {AIbitat} from '..'
* **Requires an SERPER_API_KEY environment variable**.
*
* @param query
* @param options
* @returns
*/
async function search(
+3 -2
View File
@@ -4,9 +4,9 @@ import type {AIbitat} from '..'
* A service that provides an AI client to create a completion.
*/
export abstract class Provider<T> {
private _client: T
private readonly _client: T
constructor(client: T) {
protected constructor(client: T) {
this._client = client
}
@@ -23,6 +23,7 @@ export abstract class Provider<T> {
*
* @throws known treated errors from `src/error.ts`.
* @param messages A list of messages to send.
* @param functions
*/
abstract complete(
messages: Provider.Message[],
+1
View File
@@ -52,6 +52,7 @@ export class AnthropicProvider extends Provider<Anthropic> {
* Create a completion based on the received messages.
*
* @param messages A list of messages to send to the Anthropic API.
* @param functions
* @returns The completion.
*/
async complete(
+3 -2
View File
@@ -70,6 +70,7 @@ export class OpenAIProvider extends Provider<OpenAI> {
* Create a completion based on the received messages.
*
* @param messages A list of messages to send to the OpenAI API.
* @param functions
* @returns The completion.
*/
async complete(
@@ -84,7 +85,7 @@ export class OpenAIProvider extends Provider<OpenAI> {
functions,
})
// Right now, we only support one completion
// Right now, we only support one completion,
// so we just take the first one in the list
const completion = response.choices[0].message
const cost = this.getCost(response.usage)
@@ -139,7 +140,7 @@ export class OpenAIProvider extends Provider<OpenAI> {
/**
* Get the cost of the completion.
*
* @param completion The completion to get the cost for.
* @param usage The completion to get the cost for.
* @returns The cost of the completion.
*/
getCost(usage: OpenAI.Completions.CompletionUsage | undefined) {