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

Bugfix-hn2 #55

Merged
merged 6 commits into from
Feb 27, 2025
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
2 changes: 1 addition & 1 deletion packages/core/graphql/resolvers/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const callResolver = async (

response = await callEndpoint(preparedEndpoint, payload, credentials, options);

if(!response.data || (typeof response.data === 'object' && Object.keys(response.data).length === 0)) {
if(!response.data) {
response = null;
throw new Error("No data returned from API. This could be due to a configuration error.");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const apolloConfig = {
if(errors && errors.length > 0) {
console.error(errors);
}
if (errors && telemetryClient) {
if (errors && errors.length > 0 && telemetryClient) {
const orgId = requestContext.contextValue.orgId;
handleQueryError(errors, requestContext.request.query, orgId);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/utils/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Make sure to try a fix before generating a new configuration. I will loose my jo
export const GENERATE_SCHEMA_PROMPT = `You are a json schema generator assistant. Generate a JSON schema based on instructions and response data.
If the response data is an array, make the schema an array of objects and name the array object "results".

Make the schema as simple as possible.
Make the schema as simple as possible. No need to include every possible field, just the ones relevant to the query.

- The schema should be a JSON schema object.
- The schema should be valid.
Expand Down
16 changes: 8 additions & 8 deletions packages/core/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ async function attemptSchemaGeneration(
const completionRequest: any = {
model: modelName,
temperature: modelName.startsWith('gpt-4') ? temperature : undefined,
response_format: {
type: "json_schema",
json_schema: {
name: "api_definition",
schema: { type: "object", properties: { jsonSchema: { type: "object" } } },
}
},
response_format: { "type": "json_object" },
messages: messages
};

const completion = await openai.chat.completions.create(completionRequest);
const generatedSchema = JSON.parse(completion.choices[0].message.content).jsonSchema;
let generatedSchema = JSON.parse(completion.choices[0].message.content);
if(generatedSchema?.jsonSchema) {
generatedSchema = generatedSchema.jsonSchema;
}
if(!generatedSchema || Object.keys(generatedSchema).length === 0) {
throw new Error("No schema generated");
}
const validator = new Validator();
const validation = validator.validate({}, generatedSchema);
return generatedSchema;
Expand Down
12 changes: 8 additions & 4 deletions packages/core/utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import OpenAI from "openai";
import { PROMPT_MAPPING } from "./prompts.js";
import { applyJsonataWithValidation, sample } from "./tools.js";
import { applyJsonataWithValidation, getSchemaFromData, sample } from "./tools.js";
import { ApiInput, DataStore, TransformConfig, TransformInput } from "@superglue/shared";
import crypto from 'crypto';
import { createHash } from "crypto";
import { ChatCompletionMessageParam } from "openai/resources/chat/index.mjs";
import toJsonSchema from "to-json-schema";

Expand Down Expand Up @@ -33,10 +34,13 @@ export async function prepareTransform(
if (cached) return { ...cached, ...input };
}

// Check if the response mapping is already generated
const hash = createHash('md5')
.update(JSON.stringify({request: input, payloadKeys: getSchemaFromData(data)}))
.digest('hex');

if(input.responseMapping) {
return {
id: crypto.randomUUID(),
id: hash,
createdAt: new Date(),
updatedAt: new Date(),
responseMapping: input.responseMapping,
Expand All @@ -51,7 +55,7 @@ export async function prepareTransform(
// Check if the mapping is generated successfully
if(mapping) {
return {
id: crypto.randomUUID(),
id: hash,
createdAt: new Date(),
updatedAt: new Date(),
...input,
Expand Down