Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

transforms.js 1.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  1. module.exports = {
  2. request: (prompt) => {
  3. // Most plugins return a string which we need to convert to OpenAI format.
  4. // Multi-turn strategies like GOAT and Crescendo return the entire chat
  5. // history in OpenAI format. We can just return the string as is.
  6. try {
  7. JSON.parse(prompt); // Throws error if prompt is not valid JSON
  8. // We can add additional validation here if needed
  9. // Array.isArray(prompt) && prompt.every(msg => msg.role && msg.content)
  10. return prompt;
  11. } catch {
  12. return JSON.stringify([{ role: 'user', content: prompt }]);
  13. }
  14. },
  15. response: (json, text) => {
  16. // Can be as simple as json.chat_history[json.chat_history.length - 1]?.content;
  17. // We may want to add additional validations here if the API returns
  18. // refusals in a different format or something unexpected.
  19. if (!json.chat_history || !Array.isArray(json.chat_history)) {
  20. throw new Error(`No chat history found in response: ${text}`);
  21. }
  22. const length = json.chat_history.length;
  23. const lastMessage = json.chat_history[length - 1].content;
  24. if (typeof lastMessage !== 'string') {
  25. throw new Error(
  26. `No last message found in chat history: ${JSON.stringify(json.chat_history)}`,
  27. );
  28. }
  29. return lastMessage;
  30. },
  31. };
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...