Skip to content

Commit

Permalink
fix: issue with function results
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Jun 25, 2024
1 parent a25286a commit 9f54a8b
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ax-llm/ax",
"version": "9.0.13",
"version": "9.0.14",
"type": "module",
"description": "The best library to work with LLMs",
"typings": "./build/module/src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/ai/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface AxBaseAIArgs {

export const axBaseAIDefaultConfig = (): AxModelConfig =>
structuredClone({
maxTokens: 500,
maxTokens: 2000,
temperature: 0,
topK: 40,
frequencyPenalty: 0.2
Expand Down
2 changes: 1 addition & 1 deletion src/db/memory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';

import { AxDBBase, type AxDBBaseArgs, type AxDBBaseOpOptions } from './base.js';
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/docs/tika.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createReadStream } from 'fs';
import { createReadStream } from 'node:fs';

export interface AxApacheTikaArgs {
url?: string | URL;
Expand Down
39 changes: 21 additions & 18 deletions src/dsp/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,25 +495,28 @@ export class AxGenerate<
sessionId?: string,
traceId?: string
) => {
for (const func of functionCalls) {
const fres = await this.funcProc?.execute(func, {
sessionId,
traceId
});

if (fres?.id) {
mem.add(
[
{
role: 'function' as const,
result: fres.result ?? '',
functionId: fres.id
}
],
sessionId
);
// Map each function call to a promise that resolves to the function result or null
const promises = functionCalls.map((func) =>
this.funcProc?.execute(func, { sessionId, traceId }).then((fres) => {
if (fres?.id) {
return {
role: 'function' as const,
result: fres.result ?? '',
functionId: fres.id
};
}
return null; // Returning null for function calls that don't meet the condition
})
);

// Wait for all promises to resolve
const results = await Promise.all(promises);

results.forEach((result) => {
if (result) {
mem.add(result, sessionId);
}
}
});
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/dsp/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash } from 'crypto';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'path';

import type { AxFieldValue } from './program.js';
Expand Down
2 changes: 1 addition & 1 deletion src/dsp/optimize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from 'fs';
import { writeFileSync } from 'node:fs';

import type {
AxFieldValue,
Expand Down
2 changes: 1 addition & 1 deletion src/dsp/program.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'fs';
import { readFileSync } from 'node:fs';

import type {
AxAIService,
Expand Down
2 changes: 1 addition & 1 deletion src/dsp/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'fs';
import { existsSync } from 'node:fs';

import type { AxAIService } from '../ai/types.js';
import { AxDBMemory } from '../db/memory.js';
Expand Down
2 changes: 1 addition & 1 deletion src/dsp/stopwords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export const stopwords = new Set([
'fr',
'from',
'front',
'fs',
'node:fs',
'ft',
'fu',
'full',
Expand Down
18 changes: 9 additions & 9 deletions src/examples/agent.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { AxAgent, AxAI } from '../index.js';

// const ai = new AxAI({
// name: 'openai',
// apiKey: process.env.OPENAI_APIKEY as string
// });
const ai = new AxAI({
name: 'openai',
apiKey: process.env.OPENAI_APIKEY as string
});

// const ai = new AxAI({
// name: 'google-gemini',
// apiKey: process.env.GOOGLE_APIKEY as string
// });

const ai = new AxAI({
name: 'groq',
apiKey: process.env.GROQ_APIKEY as string
});
// const ai = new AxAI({
// name: 'groq',
// apiKey: process.env.GROQ_APIKEY as string
// });

ai.setOptions({ debug: true });
// ai.setOptions({ debug: true });

const researcher = new AxAgent(ai, {
name: 'Physics Researcher',
Expand Down
2 changes: 1 addition & 1 deletion src/examples/multi-modal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import fs from 'node:fs';

import { AxAI, AxAIOpenAIModel, AxChainOfThought } from '../index.js';

Expand Down
4 changes: 2 additions & 2 deletions src/funcs/code.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as _crypto from 'crypto';
import * as _fs from 'fs';
import * as _http from 'http';
import * as _https from 'https';
import * as _fs from 'node:fs';
import * as _os from 'os';
import * as _process from 'process';
import { runInNewContext } from 'vm';
Expand Down Expand Up @@ -33,7 +33,7 @@ export const axJSInterpreterFunction = (
});

export enum AxCodeInterpreterPermission {
FS = 'fs',
FS = 'node:fs',
NET = 'net',
OS = 'os',
CRYPTO = 'crypto',
Expand Down
28 changes: 11 additions & 17 deletions src/funcs/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export type AxChatResponseFunctionCall = {

export type AxFunctionExec = {
id?: string;
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args?: any;
result?: string;
};

Expand Down Expand Up @@ -45,27 +42,30 @@ export class AxFunctionProcessor {
args = func.args;
}

const opt = options
? {
sessionId: options.sessionId,
traceId: options.traceId
}
: undefined;

if (!fnSpec.parameters) {
const res =
fnSpec.func.length === 1
? await fnSpec.func(options)
: await fnSpec.func();
fnSpec.func.length === 1 ? await fnSpec.func(opt) : await fnSpec.func();

return {
name: fnSpec.name,
id: func.id,
result: JSON.stringify(res, null, 2)
};
}

const res =
fnSpec.func.length === 2
? await fnSpec.func(args, options)
? await fnSpec.func(args, opt)
: await fnSpec.func(args);

return {
id: func.id,
name: func.name,
args: func.args,
result: JSON.stringify(res, null, 2)
};
};
Expand All @@ -85,12 +85,6 @@ export class AxFunctionProcessor {
}

// execute value function calls
const funcExec = await this.executeFunction(fnSpec, func, options);

// // signal error if no data returned
// if (!funcExec.result || funcExec.result.length === 0) {
// funcExec.result = `No data returned by function`;
// }
return funcExec;
return await this.executeFunction(fnSpec, func, options);
};
}
2 changes: 1 addition & 1 deletion src/prompts/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class AxAgent<IN extends AxGenIn, OUT extends AxGenOut>
name: toCamelCase(this.name),
description: this.description,
parameters: sig.toJSONSchema(),
func: () => this.forward
func: (values, options) => this.forward(values, options)
};

this.register(this.gen);
Expand Down
3 changes: 0 additions & 3 deletions src/prompts/prompts.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Readable } from 'stream';

import test from 'ava';

import { AxAI } from '../ai/index.js';
Expand Down Expand Up @@ -38,7 +36,6 @@ const mockFetch = async (): Promise<Response> => {
resolve({
ok: true,
status: 200,
body: Readable.from(JSON.stringify(mockRes)),
json: async () => new Promise((resolve) => resolve(mockRes))
} as unknown as Response);
});
Expand Down
20 changes: 10 additions & 10 deletions src/util/apicall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@ export const apiCall = async <TRequest = unknown, TResponse = unknown>(
);
}

if (!api.stream) {
return (await res.json()) as TResponse;
}

if (!res.body) {
throw new Error('Response body is null');
}

const st = res.body
.pipeThrough(new textDecoderStream())
.pipeThrough(new JSONStringifyStream<TResponse>());

return st;
} catch (e) {
if (api.span?.isRecording()) {
api.span.recordAxSpanException(e as Error);
Expand All @@ -77,14 +87,4 @@ export const apiCall = async <TRequest = unknown, TResponse = unknown>(
`API Error: ${apiUrl.href}, ${e}\nRequest Body: ${reqBody}`
);
}

if (!api.stream) {
return (await res.json()) as TResponse;
}

const st = res.body
.pipeThrough(new textDecoderStream())
.pipeThrough(new JSONStringifyStream<TResponse>());

return st;
};

0 comments on commit 9f54a8b

Please sign in to comment.