-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from samchon/feat/separate
Fix `IHttpLlmFunction.seperated` composing bug.
- Loading branch information
Showing
11 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import OpenAI from "openai"; | ||
import typia, { IValidation, tags } from "typia"; | ||
|
||
interface IMember { | ||
email: string & tags.Format<"email">; | ||
name: string; | ||
age: number; | ||
hobbies: string[]; | ||
joined_at: string & tags.Format<"date">; | ||
} | ||
|
||
const step = async ( | ||
failure?: IValidation.IFailure | undefined, | ||
): Promise<IValidation<IMember>> => { | ||
const client: OpenAI = new OpenAI({ | ||
apiKey: "<YOUR_OPENAI_API_KEY>", | ||
}); | ||
const completion: OpenAI.ChatCompletion = | ||
await client.chat.completions.create({ | ||
model: "gpt-4o", | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: [ | ||
"I am a new member of the community.", | ||
"", | ||
"My name is John Doe, and I am 25 years old.", | ||
"I like playing basketball and reading books,", | ||
"and joined to this community at 2022-01-01.", | ||
].join("\n"), | ||
}, | ||
...(failure | ||
? [ | ||
{ | ||
role: "system", | ||
content: [ | ||
"You A.I. agent had taken a mistak that", | ||
"returing wrong typed structured data.", | ||
"", | ||
"Here is the detailed list of type errors.", | ||
"Review and correct them at the next step.", | ||
"", | ||
"```json", | ||
JSON.stringify(failure.errors, null, 2), | ||
"```", | ||
].join("\n"), | ||
} satisfies OpenAI.ChatCompletionSystemMessageParam, | ||
] | ||
: []), | ||
], | ||
response_format: { | ||
type: "json_schema", | ||
json_schema: { | ||
name: "member", | ||
schema: typia.llm.parameters<IMember, "chatgpt">() as any, | ||
}, | ||
}, | ||
}); | ||
const member: IMember = JSON.parse(completion.choices[0].message.content!); | ||
return typia.validate(member); | ||
}; | ||
|
||
const main = async (): Promise<void> => { | ||
let result: IValidation<IMember> | undefined = undefined; | ||
for (let i: number = 0; i < 3; ++i) { | ||
if (result && result.success === true) break; | ||
result = await step(result); | ||
} | ||
console.log(result); | ||
}; | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { TestValidator } from "@nestia/e2e"; | ||
import { | ||
HttpLlm, | ||
IHttpLlmApplication, | ||
ILlmSchema, | ||
OpenApi, | ||
OpenApiV3, | ||
OpenApiV3_1, | ||
SwaggerV2, | ||
} from "@samchon/openapi"; | ||
import { LlmSchemaComposer } from "@samchon/openapi/lib/composers/LlmSchemaComposer"; | ||
import { Singleton } from "tstl"; | ||
import typia from "typia"; | ||
|
||
export const test_chatgpt_application_separate = async (): Promise<void> => { | ||
await validate_llm_application_separate("chatgpt", false); | ||
await validate_llm_application_separate("chatgpt", true); | ||
}; | ||
|
||
export const test_claude_application_separate = async (): Promise<void> => { | ||
await validate_llm_application_separate("claude", false); | ||
await validate_llm_application_separate("claude", true); | ||
}; | ||
|
||
export const test_gemini_application_separate = async (): Promise<void> => { | ||
await validate_llm_application_separate("gemini", false); | ||
}; | ||
|
||
export const test_llama_application_separate = async (): Promise<void> => { | ||
await validate_llm_application_separate("llama", false); | ||
await validate_llm_application_separate("llama", true); | ||
}; | ||
|
||
export const test_llm_v30_application_separate = async (): Promise<void> => { | ||
await validate_llm_application_separate("3.0", false); | ||
await validate_llm_application_separate("3.0", true); | ||
}; | ||
|
||
export const test_llm_v31_application_separate = async (): Promise<void> => { | ||
await validate_llm_application_separate("3.1", false); | ||
await validate_llm_application_separate("3.1", true); | ||
}; | ||
|
||
const validate_llm_application_separate = async < | ||
Model extends ILlmSchema.Model, | ||
>( | ||
model: Model, | ||
constraint: boolean, | ||
): Promise<void> => { | ||
const application: IHttpLlmApplication<Model> = HttpLlm.application({ | ||
model, | ||
document: await document.get(), | ||
options: { | ||
separate: (schema: any) => | ||
LlmSchemaComposer.typeChecker(model).isString(schema as any) && | ||
(schema as any)["x-wrtn-secret-key"] !== undefined, | ||
constraint: constraint as any, | ||
} as any, | ||
}); | ||
for (const func of application.functions) | ||
TestValidator.equals("separated")(!!func.separated)(true); | ||
}; | ||
|
||
const document = new Singleton(async (): Promise<OpenApi.IDocument> => { | ||
const swagger: | ||
| SwaggerV2.IDocument | ||
| OpenApiV3.IDocument | ||
| OpenApiV3_1.IDocument = await fetch( | ||
"https://wrtnio.github.io/connectors/swagger/swagger.json", | ||
).then((r) => r.json()); | ||
return OpenApi.convert(typia.assert(swagger)); | ||
}); |