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

Optimize assistant dynamic prompt #1135

Open
xbol0 opened this issue May 13, 2024 · 0 comments
Open

Optimize assistant dynamic prompt #1135

xbol0 opened this issue May 13, 2024 · 0 comments

Comments

@xbol0
Copy link

xbol0 commented May 13, 2024

Assistant dynamic prompt is an awesome feature, I read the implementation at

if (assistantHasDynamicPrompt && preprompt) {

I felt its implementation is a bit too simple, here's my some suggestions (from line 385):

// The result always replace to system prompt,
// so we can skip the data retrieve if there do not provided system prompt
if (assistantHasDynamicPrompt && preprompt && messagesForPrompt[0].from === "system") {
  // process the preprompt
  const urlRegex = /{{\s?url=(.*?)\s?}}/g;

  // define a max loop count
  // This is a recursive loop,
  // If the result contains the '{{url=xxxx}}' format with same content url,
  // it will diving into a infinite loop
  let match, maxLoop = 10; // it can be define at other files
  while ((match = urlRegex.exec(preprompt)) !== null && maxLoop--) {
  try {
  // Add programmable placeholder for more context,
  // It's very useful because the user prompt is always provided,
  // the service can return the targeted result.
  // The assistant's author can configure it as a path parameter or a query
  // eg. https://xxx.com/%S or https://xxx.com/?q=%S
  let preUrl = match[1];
  preUrl = preUrl.replaceAll('%S', messagesForPrompt[0].content) // may it should be encoded
  // Other placeholder
  // Such as %R for random seed, bypass cache
  // %T for timestamp
  const url = new URL(preUrl);
  if (await isURLLocal(url)) {
	  throw new Error("URL couldn't be fetched, it resolved to a local address.");
  }
  
  // Add more identity headers
  // If the service contains many response format,
  // these headers can tell it which correct format should be returned
  const res = await fetch(url.href, {
    headers: {
      "accept": "text/plain, text/markdown, text/*;q=0.9, */*;q=0.8",
      "user-agent": "ChatUI/0.8",
    },
  });
  
  if (!res.ok) {
	  throw new Error("URL couldn't be fetched, error " + res.status);
  }
  const text = await res.text();
  preprompt = preprompt.replaceAll(match[0], text);
  } catch (e) {
  preprompt = preprompt.replaceAll(match[0], (e as Error).message);
  }
  }
  
  // if (messagesForPrompt[0].from === "system") {
  messagesForPrompt[0].content = preprompt;
  //}
  }
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

No branches or pull requests

1 participant