Open SebastianStehle opened 3 months ago
To address the issue with the URL discrepancy when using Dall-E through OpenAI tools in LangChain.js, you might consider implementing a custom solution to bypass the LLM for specific tool calls. This involves directly handling the API requests to Dall-E, including managing the query parameters that are missing in the URL provided by OpenAI.
Here's a conceptual approach:
Directly Use OpenAI API for Dall-E Requests: Implement a conditional logic in your application to directly invoke the OpenAI API for Dall-E image generation requests, bypassing the LangChain.js LLM wrapper for these calls. This allows you to manually include all necessary query parameters in the request.
Custom Handling for Dall-E Requests: You can add a custom method within your application or extend the OpenAI
class in LangChain.js to handle Dall-E requests. This method would construct the request URL with the required query parameters and make the API call directly, ensuring the URL is correctly formatted.
Here's an example of how you might implement such a method:
async generateDallEImage(prompt: string, options?: any): Promise<any> {
const dallEUrl = `https://api.openai.com/v1/dalle/generate?prompt=${encodeURIComponent(prompt)}&your_query_param=your_value`;
const requestOptions = {
headers: {
"Authorization": `Bearer ${this.apiKey}`,
},
};
try {
const response = await fetch(dallEUrl, requestOptions);
const data = await response.json();
return data;
} catch (error) {
console.error("Error generating Dall-E image:", error);
throw error;
}
}
This method directly constructs the request URL, including any necessary query parameters, and uses fetch
to make the HTTP request. Adjust this example based on your specific environment and requirements.
Implementing a direct API call approach allows you to bypass limitations related to URL formatting and ensures that your Dall-E requests include all necessary information.
Checked other resources
Example Code
I am not sure if this is a bug or a feature request. But anyway. I am using Dall-E with openai tools. My code looks similar to this:
Error Message and Stack Trace (if applicable)
No response
Description
Usually OpenAI shows the URL, but query params are not included and the final URL differs from the result that Dall-E provides. This seems to be some kind of Spam protection and makes several tool scenarios difficult or impossible.
I could not find a good workaround yet. My idea was to encode the URL in a way that OpenAI does not understand and then decode it later, but it would not very bullet proof. So ultimately I think there should be a way to bypass the LLM for some tool calls.
System Info
Langchain 0.1.33