Skip to content

Commit

Permalink
Merge pull request #55 from superglue-ai/bugfix-hn2
Browse files Browse the repository at this point in the history
Bugfix-hn2
  • Loading branch information
stefanfaistenauer authored Feb 27, 2025
2 parents 7295381 + 52a871f commit 560565a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
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

0 comments on commit 560565a

Please sign in to comment.