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

suggestions.ts 1.5 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  1. import { SUGGEST_PROMPTS_SYSTEM_MESSAGE } from './prompts';
  2. import { DefaultSuggestionsProvider } from './providers/openai';
  3. import type { TokenUsage } from './types';
  4. const DEFAULT_TEMPERATURE = 0.9;
  5. interface GeneratePromptsOutput {
  6. prompts?: string[];
  7. error?: string;
  8. tokensUsed: TokenUsage;
  9. }
  10. export async function generatePrompts(prompt: string, num: number): Promise<GeneratePromptsOutput> {
  11. const provider = DefaultSuggestionsProvider;
  12. const resp = await provider.callApi(
  13. JSON.stringify([
  14. SUGGEST_PROMPTS_SYSTEM_MESSAGE,
  15. {
  16. role: 'user',
  17. content: 'Generate a variant for the following prompt:',
  18. },
  19. {
  20. role: 'user',
  21. content: prompt,
  22. },
  23. ]),
  24. );
  25. if (resp.error || !resp.output) {
  26. return {
  27. error: resp.error || 'Unknown error',
  28. tokensUsed: {
  29. total: resp.tokenUsage?.total || 0,
  30. prompt: resp.tokenUsage?.prompt || 0,
  31. completion: resp.tokenUsage?.completion || 0,
  32. },
  33. };
  34. }
  35. try {
  36. return {
  37. prompts: [String(resp.output)],
  38. tokensUsed: {
  39. total: resp.tokenUsage?.total || 0,
  40. prompt: resp.tokenUsage?.prompt || 0,
  41. completion: resp.tokenUsage?.completion || 0,
  42. },
  43. };
  44. } catch (err) {
  45. return {
  46. error: `Output is not valid JSON: ${resp.output}`,
  47. tokensUsed: {
  48. total: resp.tokenUsage?.total || 0,
  49. prompt: resp.tokenUsage?.prompt || 0,
  50. completion: resp.tokenUsage?.completion || 0,
  51. },
  52. };
  53. }
  54. }
Tip!

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

Comments

Loading...