Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More example cases for the ChatGPT function calling. #75

Merged
merged 3 commits into from
Nov 20, 2024
Merged
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
145 changes: 37 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ In `@samchon/openapi`, you can execute the LLM function calling by `HttpLlm.exec
```typescript
import {
HttpLlm,
IChatGptSchema,
IHttpLlmApplication,
IHttpLlmFunction,
ILlmSchemaV3_1,
OpenApi,
OpenApiV3,
OpenApiV3_1,
Expand All @@ -277,16 +277,17 @@ const main = async (): Promise<void> => {
// convert to emended OpenAPI document,
// and compose LLM function calling application
const document: OpenApi.IDocument = OpenApi.convert(swagger);
const application: IHttpLlmApplication<"3.1"> = HttpLlm.application({
model: "3.1",
const application: IHttpLlmApplication<"chatgpt"> = HttpLlm.application({
model: "chatgpt",
document,
});

// Let's imagine that LLM has selected a function to call
const func: IHttpLlmFunction<ILlmSchemaV3_1> | undefined = application.functions.find(
// (f) => f.name === "llm_selected_fuction_name"
(f) => f.path === "/bbs/{section}/articles/{id}" && f.method === "put",
);
const func: IHttpLlmFunction<IChatGptSchema.IParameters> | undefined =
application.functions.find(
// (f) => f.name === "llm_selected_fuction_name"
(f) => f.path === "/bbs/{section}/articles/{id}" && f.method === "put",
);
if (func === undefined) throw new Error("No matched function exists.");

// actual execution is by yourself
Expand All @@ -296,94 +297,19 @@ const main = async (): Promise<void> => {
},
application,
function: func,
arguments: [
"general",
v4(),
{
input: {
section: "general",
id: v4(),
query: {
language: "en-US",
format: "markdown",
},
body: {
title: "Hello, world!",
body: "Let's imagine that this argument is composed by LLM.",
thumbnail: null,
},
],
});
console.log("article", article);
};
main().catch(console.error);
```

### Keyword Parameter
Combine parameters into single object.

If you configure `keyword` option when composing the LLM (Large Language Model) function calling appliation, every parameters of OpenAPI operations would be combined to a single object type in the LLM funtion calling schema. This strategy is loved in many A.I. Chatbot developers, because LLM tends to a little professional in the single parameter function case.

Also, do not worry about the function call execution case. You don't need to resolve the keyworded parameter manually. The `HttpLlm.execute()` and `HttpLlm.propagate()` functions will resolve the keyworded parameter automatically by analyzing the `IHttpLlmApplication.options` property.

```typescript
import {
HttpLlm,
IHttpLlmApplication,
IHttpLlmFunction,
ILlmSchemaV3_1,
OpenApi,
OpenApiV3,
OpenApiV3_1,
SwaggerV2,
} from "@samchon/openapi";
import fs from "fs";
import typia from "typia";
import { v4 } from "uuid";

const main = async (): Promise<void> => {
// read swagger document and validate it
const swagger:
| SwaggerV2.IDocument
| OpenApiV3.IDocument
| OpenApiV3_1.IDocument = JSON.parse(
await fs.promises.readFile("swagger.json", "utf8"),
);
typia.assert(swagger); // recommended

// convert to emended OpenAPI document,
// and compose LLM function calling application
const document: OpenApi.IDocument = OpenApi.convert(swagger);
const application: IHttpLlmApplication<"3.1"> = HttpLlm.application({
model: "3.1",
document,
options: {
keyword: true,
},
});

// Let's imagine that LLM has selected a function to call
const func: IHttpLlmFunction<ILlmSchemaV3_1> | undefined = application.functions.find(
// (f) => f.name === "llm_selected_fuction_name"
(f) => f.path === "/bbs/{section}/articles/{id}" && f.method === "put",
);
if (func === undefined) throw new Error("No matched function exists.");

// actual execution is by yourself
const article = await HttpLlm.execute({
connection: {
host: "http://localhost:3000",
},
application,
function: func,
arguments: [
// one single object with key-value paired
{
section: "general",
id: v4(),
query: {
language: "en-US",
format: "markdown",
},
body: {
title: "Hello, world!",
body: "Let's imagine that this argument is composed by LLM.",
thumbnail: null,
},
},
],
});
console.log("article", article);
};
Expand All @@ -402,10 +328,10 @@ Here is the example code separating the file uploading feature from the LLM func
```typescript
import {
HttpLlm,
ChatGptTypeChecker,
IChatGptSchema,
IHttpLlmApplication,
IHttpLlmFunction,
ILlmSchemaV3_1,
LlmTypeCheckerV3_1,
OpenApi,
OpenApiV3,
OpenApiV3_1,
Expand All @@ -428,21 +354,22 @@ const main = async (): Promise<void> => {
// convert to emended OpenAPI document,
// and compose LLM function calling application
const document: OpenApi.IDocument = OpenApi.convert(swagger);
const application: IHttpLlmApplication<"3.1"> = HttpLlm.application({
model: "3.1",
const application: IHttpLlmApplication<"chatgpt"> = HttpLlm.application({
model: "chatgpt",
document,
options: {
keyword: false,
separate: (schema) =>
LlmTypeCheckerV3_1.isString(schema) && schema.contentMediaType !== undefined,
ChatGptTypeChecker.isString(schema) && schema.contentMediaType !== undefined,
},
});

// Let's imagine that LLM has selected a function to call
const func: IHttpLlmFunction<ILlmSchemaV3_1> | undefined = application.functions.find(
// (f) => f.name === "llm_selected_fuction_name"
(f) => f.path === "/bbs/articles/{id}" && f.method === "put",
);
const func: IHttpLlmFunction<IChatGptSchema.IParameters> | undefined =
application.functions.find(
// (f) => f.name === "llm_selected_fuction_name"
(f) => f.path === "/bbs/articles/{id}" && f.method === "put",
);
if (func === undefined) throw new Error("No matched function exists.");

// actual execution is by yourself
Expand All @@ -454,23 +381,25 @@ const main = async (): Promise<void> => {
function: func,
arguments: HttpLlm.mergeParameters({
function: func,
llm: [
llm: {
// LLM composed parameter values
"general",
v4(),
{
section: "general",
id: v4(),
query: {
language: "en-US",
format: "markdown",
},
{
body: {
title: "Hello, world!",
content: "Let's imagine that this argument is composed by LLM.",
},
],
human: [
},
human: {
// Human composed parameter values
{ thumbnail: "https://example.com/thumbnail.jpg" },
],
body: {
thumbnail: "https://example.com/thumbnail.jpg",
},
},
}),
});
console.log("article", article);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "example",
"age": 42
"age": 25
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,121 @@
"name": "electronics",
"children": [
{
"id": "2",
"id": "11",
"name": "desktops",
"children": []
},
{
"id": "3",
"id": "12",
"name": "laptops",
"children": [
{
"id": "4",
"id": "121",
"name": "ultrabooks",
"children": []
},
{
"id": "5",
"id": "122",
"name": "macbooks",
"children": []
},
{
"id": "6",
"id": "123",
"name": "desknotes",
"children": []
},
{
"id": "7",
"id": "124",
"name": "2 in 1 laptops",
"children": []
}
]
},
{
"id": "8",
"id": "13",
"name": "tablets",
"children": [
{
"id": "9",
"id": "131",
"name": "ipads",
"children": []
},
{
"id": "10",
"id": "132",
"name": "android tablets",
"children": []
},
{
"id": "11",
"id": "133",
"name": "windows tablets",
"children": []
}
]
},
{
"id": "12",
"id": "14",
"name": "smartphones",
"children": [
{
"id": "13",
"id": "141",
"name": "mini smartphones",
"children": []
},
{
"id": "14",
"id": "142",
"name": "phablets",
"children": []
},
{
"id": "15",
"id": "143",
"name": "gaming smartphones",
"children": []
},
{
"id": "16",
"id": "144",
"name": "rugged smartphones",
"children": []
},
{
"id": "17",
"id": "145",
"name": "foldable smartphones",
"children": []
}
]
},
{
"id": "18",
"id": "15",
"name": "cameras",
"children": []
},
{
"id": "19",
"id": "16",
"name": "televisions",
"children": []
}
]
},
{
"id": "20",
"id": "2",
"name": "furnitures",
"children": []
},
{
"id": "21",
"id": "3",
"name": "accessories",
"children": [
{
"id": "22",
"id": "31",
"name": "jewelry",
"children": []
},
{
"id": "23",
"id": "32",
"name": "clothing",
"children": []
},
{
"id": "24",
"id": "33",
"name": "shoes",
"children": []
}
Expand Down
Loading