Skip to content

Commit

Permalink
Merge pull request #4 from IdeaLeap/dev
Browse files Browse the repository at this point in the history
migrate openai-node to v4.0.1
  • Loading branch information
MarleneJiang authored Aug 20, 2023
2 parents b2c9b85 + a7a007a commit b811a28
Show file tree
Hide file tree
Showing 31 changed files with 1,504 additions and 904 deletions.
15 changes: 12 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module.exports = {
files: ["*.js"],
extends: ["plugin:@typescript-eslint/disable-type-checked"],
},
{
files: [
'**/__tests__/*.{j,t}s?(x)',
'**/tests/unit/**/*.spec.{j,t}s?(x)'
],
env: {
jest: true
}
},
],
parserOptions: {
project: './tsconfig.json',
Expand All @@ -29,12 +38,12 @@ module.exports = {
"prettier/prettier": "error",
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unsafe-assignment": 0,
"@typescript-eslint/no-unsafe-member-access": "warn",
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/no-unsafe-member-access": 0,
"@typescript-eslint/no-empty-function": 0,
"no-constant-condition": 0,
"@typescript-eslint/no-unsafe-call": 0,
"@typescript-eslint/no-unsafe-argument": 0,
"@typescript-eslint/no-unsafe-return": "warn",
"@typescript-eslint/no-unsafe-return": 0,
"@typescript-eslint/no-redundant-type-constituents": 0,
"no-debugger":0,
"@typescript-eslint/no-unsafe-member-access":0,
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<img src="https://wakatime.com/badge/user/5bfd81bc-9515-462b-a942-069791b283b7/project/af5f20a2-48c4-4ffb-81b8-7c330a9ee330.svg?style=flat-square" alt="Develop time"/>

</p>
<p align="center">基于GWT理论构建的LLM Agent 智能系统框架,不基于LangchainJs!</p>
<p align="center">基于GWT理论构建的 LLM Agent 智能系统框架,不基于LangchainJs!</p>

## 🎨 技术栈

- openai
- Openai
- Jest
- Typescript
- Vitepress
Expand All @@ -20,6 +20,19 @@
- Github Actions
- Vercel Ncc

## Misc

发包时由于使用 ES module,Node.js 内置的 __filename 与__dirname 就变得不可用了,需要使用 url 模块的 fileURLToPath 方法将 import.meta.url 转换为文件路径,然后再使用 path 模块的 dirname 方法获取文件所在目录的路径。

```js
import {fileURLToPath} from 'url';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

```
## 📄 作者
(C) 2023 Marlene, Idealeap
17 changes: 12 additions & 5 deletions docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ const reqMessages: messagesType = [
];

const res = await llm.chat({
choice_num: 3,
messages: reqMessages,
});
llm.printMessage(res.choices, reqMessages);
llm.printMessage();
console.log(res);
debugger;

Expand Down Expand Up @@ -109,14 +108,22 @@ const reqMessages: messagesType = [
];

const res = await llm.chat({
choice_num: 3,
messages: reqMessages,
functions: functions,
function_call: { name: "get_current_weather" },
});
llm.printMessage(res.choices, reqMessages);
llm.printMessage();
console.log(res);
debugger;
```

## Moderation
## Moderation & Embedding

```ts
import { LLM } from "../index.js";
const llm = new LLM({});
const res = await llm.embedding("hello world");
console.log(res.data[0].embedding);
debugger;

```
2 changes: 1 addition & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ module.exports = {
],
setupFiles: ["dotenv/config"],
testTimeout: 20_000,
};
};
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "gwt",
"version": "1.0.0",
"name": "@idealeap/gwt",
"version": "0.0.2-beta.1",
"description": "",
"engines": {
"node": ">=18"
},
"main": "index.js",
"main": "package/index.js",
"scripts": {
"start": "ts-node --esm ./package/index.ts",
"test": "jest --coverage",
Expand All @@ -22,21 +22,23 @@
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"@vercel/ncc": "^0.36.1",
"@zilliz/milvus2-sdk-node": "^2.2.24",
"dotenv": "^16.3.1",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^5.0.0",
"node-fetch": "^3.3.2",
"openai": "4.0.0-beta.6",
"node-fetch": "2",
"openai": "4.0.1",
"prettier": "^3.0.0"
},
"devDependencies": {
"@idealeap/gwt": "link:./package",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.2",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-promise": "^6.0.0",
"jest": "^29.6.1",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "^2.6.0",
Expand Down
1 change: 1 addition & 0 deletions package/agents/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./teacherAgents/index.js";
46 changes: 29 additions & 17 deletions package/agents/teacherAgents/__tests__/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { BaseAgent } from "../agent.js";
import fetch from "node-fetch";
const data = (await (
await fetch(
"https://idealeap-1254110372.cos.ap-shanghai.myqcloud.com/Other/agent.json",
)
).json()) as { agents: any[] };
const agent = new BaseAgent(data.agents[0].params);
const res = await agent.call(
`你好,非常欢迎你来到我身边~我是xxx,我会把最美好的一面呈现给你!`,
);
if (!res.success) {
console.error(res.message);
} else {
console.log(res.data);
}
debugger;
import { BaseAgent } from "@idealeap/gwt";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetch = require("node-fetch");
test("参数从远程载入", async () => {
const data = (await (
await fetch("https://cos.idealeap.cn/Other/agent.json")
).json()) as { agents: any[] };
const agent = new BaseAgent(data.agents[0].params);
const res = await agent.call({
request: `你好,非常欢迎你来到我身边~我是xxx,我会把最美好的一面呈现给你!`,
prompts: data.agents[0].params.prompts,
struct: data.agents[0].params.struct,
});
if (!res.success) {
console.error(res.message);
} else {
console.log(res.data);
}
debugger;

// system : You are an article polishing teacher, the main task is polishing PPT speech draft.I'll give you a speech, you need to polish it up. Ensure the consistency, logic and grammatical correctness of the speech. Please use Encouraging language to fully reflect the brief nature of the PPT presentation. Whatever I offer, I must answer in Chinese. After answering, please make an objective evaluation of the results. The rating items are [audience_understandability,logicality]Finally, according to the score and results, the next improvement suggestions are given.

// user:
// 你好,非常欢迎你来到我身边~我是xxx,我会把最美好的一面呈现给你!

// assistant: {"name":"result_evaluation","arguments":"{\n \"polished_result\": \"你好,非常欢迎你来到我身边!我是xxx,我将为你展示我最美好的一面!\",\n \"audience_understandability\": 90,\n \"logicality\": 90,\n \"suggestion\": \"no suggestion\"\n } "}

// {polished_result: '你好,非常欢迎你来到我身边!我是xxx,我将为你展示我最美好的一面!', audience_understandability: 90, logicality: 90, suggestion: 'no suggestion'}
});
33 changes: 0 additions & 33 deletions package/agents/teacherAgents/__tests__/index.test.ts

This file was deleted.

89 changes: 43 additions & 46 deletions package/agents/teacherAgents/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import {
functionsType,
function_callType,
} from "../../utils/index.js";
import { messageType } from "../../attention/index.js";
import { messageType } from "../../utils/index.js";
import { FunctionChain, TypeScriptChain } from "../../motion/index.js";
import {
PolishPromptTemplate,
AgentPromptTemplate,
} from "../../attention/index.js";
export interface BaseAgentSchema {
llmSchema?: createLLMSchema;
prompts?: PromptsSchema;
chainName?: string;
}

export interface BaseAgentCallSchema {
request: messageType | string;
prompts?: PromptsSchema;
struct?: structSchema;
}

Expand All @@ -31,14 +35,29 @@ export interface structSchema {
schema?: string;
typeName?: string;
}
// 初始化输入llmSchema,prompt,request,chainName,schema 输出result
export class BaseAgent<T> {
export class BaseAgent {
llm: LLM;
prompt?: messageType[];
chain?: T;
constructor(json: object) {
const { llmSchema, prompts, chainName, struct } = json as BaseAgentSchema;
chain?: any;
chainName: string;
constructor(params: BaseAgentSchema) {
const { llmSchema, chainName } = params;
this.llm = new LLM(llmSchema || {});
this.chainName = chainName || "";
switch (this.chainName) {
case "typeChat":
this.chain = new TypeScriptChain(this.llm);
break;
default:
this.chain = new FunctionChain(this.llm);
break;
}
}
async call(params: BaseAgentCallSchema): Promise<Result<any>> {
const { request, prompts, struct } = params;
if (!this.chain) {
return error("Chain not initialized");
}
switch (prompts?.name) {
case "polishPromptTemplate":
this.prompt = new PolishPromptTemplate(prompts.schema).format();
Expand All @@ -56,49 +75,27 @@ export class BaseAgent<T> {
this.prompt = prompts?.prompt || [];
break;
}
if (!chainName || !struct) {
this.chain = FunctionChain(this.llm) as unknown as T;
return;
}
switch (chainName) {
let result = undefined;
switch (this.chainName) {
case "typeChat":
if (!struct.schema || !struct.typeName) {
this.chain = FunctionChain(this.llm) as unknown as T;
break;
}
this.chain = TypeScriptChain(
this.llm,
struct.schema,
struct.typeName,
true,
) as unknown as T;
result = await this.chain.call({
request,
prompt: this.prompt,
schema: struct?.schema,
typeName: struct?.typeName,
verbose: true,
});
break;
case "function":
if (!struct.functions || !struct.function_call) {
this.chain = FunctionChain(this.llm) as unknown as T;
break;
}
this.chain = FunctionChain(
this.llm,
struct.functions,
struct.function_call,
true,
) as unknown as T;
default:
result = await this.chain.call({
request,
prompt: this.prompt,
functions: struct?.functions,
function_call: struct?.function_call,
verbose: true,
});
break;
}
}
async call(request: messageType | string): Promise<Result<T>> {
if (!this.chain) {
return error("Chain not initialized");
}
const result = await (
this.chain as unknown as {
call: (
request: messageType | string,
prompt?: messageType[],
) => Promise<Result<T>>;
}
).call(request, this.prompt);
return result;
}
}
1 change: 1 addition & 0 deletions package/agents/teacherAgents/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./agent.js";
Loading

0 comments on commit b811a28

Please sign in to comment.