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

switched from brian AI to Openai #35

Merged
merged 7 commits into from
Jan 24, 2025

Conversation

TheRanomial
Copy link
Contributor

@TheRanomial TheRanomial commented Jan 23, 2025

fixes #21
This is the main function

async function getTransactionIntentFromOpenAI(
  prompt: string,
  address: string,
  chainId: string,
  messages: any[]
): Promise<BrianResponse> {
  try {
    const response = await openai.chat.completions.create({
      model: "gpt-4o-mini",
      response_format: { type: "json_object" },
      messages: [
        {
          role: "system",
          content: `${TRANSACTION_INTENT_PROMPT}\n\nAdditional Context:\nCurrent Chain ID: ${chainId}`,
        },
        { role: "user", content: prompt },
        ...messages.map((msg) => ({
          role: msg.role,
          content: msg.content,
        })),
      ],
    });

    const intentData = JSON.parse(response.choices[0].message.content || "{}");

    if (!intentData.isTransactionIntent) {
      throw new Error("Not a transaction-related prompt");
    }

    const brianResponse: BrianResponse = {
      solver: intentData.solver || "OpenAI-Intent-Recognizer",
      action: intentData.action,
      type: "write",
      extractedParams: {
        action: intentData.extractedParams.action,
        token1: intentData.extractedParams.token1 || "",
        token2: intentData.extractedParams.token2 || "",
        chain: intentData.extractedParams.chain || "",
        dest_chain: intentData.extractedParams.dest_chain || "",
        amount: intentData.extractedParams.amount || "",
        protocol: intentData.extractedParams.protocol || "",
        address: intentData.extractedParams.address || address,
        destinationAddress:
          intentData.extractedParams.destinationAddress || address,
        destinationChain: intentData.extractedParams.dest_chain || "",
      },
      data: {} as BrianTransactionData,
    };

    switch (brianResponse.action) {
      case "swap":
      case "transfer":
        brianResponse.data = {
          description: "",
          steps: [],
          fromToken: {
            symbol: brianResponse.extractedParams?.token1 || "",
            address: brianResponse.extractedParams?.address || "",
            decimals: 1, // default adjust if needed
          },
          toToken: {
            symbol: brianResponse.extractedParams?.token2 || "",
            address: brianResponse.extractedParams?.address || "",
            decimals: 1, // default adjust if needed
          },
          fromAmount: brianResponse.extractedParams?.amount,
          toAmount: brianResponse.extractedParams?.amount,
          receiver: brianResponse.extractedParams?.address,
        };
        break;

      case "bridge":
        brianResponse.data = {
          description: "",
          steps: [],
          bridge: {
            sourceNetwork: brianResponse.extractedParams?.chain || "",
            destinationNetwork: brianResponse.extractedParams
              ?.dest_chain as string,
            sourceToken: brianResponse.extractedParams?.token1 || "",
            destinationToken: brianResponse.extractedParams?.token2 || "",
            amount: parseFloat(brianResponse.extractedParams?.amount || "0"),
            sourceAddress: address,
            destinationAddress:
              brianResponse.extractedParams?.destinationAddress || "",
          },
        };
        break;

      case "deposit":
      case "withdraw":
        brianResponse.data = {
          description: "",
          steps: [],
          protocol: brianResponse.extractedParams?.protocol,
          fromAmount: brianResponse.extractedParams?.amount,
          toAmount: brianResponse.extractedParams?.amount,
          receiver: brianResponse.extractedParams?.address,
        };
        break;

      default:
        throw new Error(`Unsupported action type: ${brianResponse.action}`);
    }

    return brianResponse;
  } catch (error) {
    console.error("Error fetching transaction intent:", error);
    throw error;
  }
}

let me know i need to change the code or make any new changes.

Copy link

vercel bot commented Jan 23, 2025

@TheRanomial is attempting to deploy a commit to the poulavbhowmick03's projects Team on Vercel.

A member of the Team first needs to authorize it.

@PoulavBhowmick03
Copy link
Contributor

@TheRanomial you need to change all the brian types (be it just the names or the types altogether) and have it use the object provided by OpenAI. Check the lib folder.

@guha-rahul
Copy link
Contributor

@tosoham tosoham self-requested a review January 23, 2025 14:41
@PoulavBhowmick03
Copy link
Contributor

@TheRanomial did u test it out?
Because when I try it out locally, I get this error
image

image

Can you share a screen recording of the working?

Copy link
Contributor

@PoulavBhowmick03 PoulavBhowmick03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send a video or run it locally to test it out

@TheRanomial
Copy link
Contributor Author

Please send a video or run it locally to test it out

is there any way i can only run the frontend becausei don't have the credentials to run the backend services

@tosoham
Copy link
Contributor

tosoham commented Jan 23, 2025

Please send a video or run it locally to test it out

is there any way i can only run the frontend becausei don't have the credentials to run the backend services

Ohh man you should have asked dm me in telegram with usrname: @tosoham

@tosoham
Copy link
Contributor

tosoham commented Jan 24, 2025

looks good

@tosoham tosoham dismissed stale reviews from PoulavBhowmick03 and themself January 24, 2025 11:16

resolved

@tosoham tosoham merged commit 0eb4c55 into Shonen-Labs:main Jan 24, 2025
2 of 4 checks passed
@tosoham
Copy link
Contributor

tosoham commented Jan 24, 2025

@TheRanomial from next time I won't appreciate any screenshots you have 3 hours to completely fix all the issues and errors in the transaction...make a video and share in github or me personally. Otherwise you will be unassigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change BrianAI API call to OpenAI
4 participants