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

prompt.py 2.7 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  1. import base64
  2. import typing
  3. import requests
  4. # Type definitions for improved code readability
  5. class Vars(typing.TypedDict):
  6. image_url: str
  7. class Provider(typing.TypedDict):
  8. id: str
  9. label: str | None
  10. class PromptFunctionContext(typing.TypedDict):
  11. vars: Vars
  12. provider: Provider
  13. def get_image_base64(image_url: str) -> str:
  14. """
  15. Fetch an image from a URL and convert it to a base64-encoded string.
  16. Args:
  17. image_url (str): The URL of the image to fetch.
  18. Returns:
  19. str: The base64-encoded image data.
  20. """
  21. response = requests.get(image_url)
  22. return base64.b64encode(response.content).decode("utf-8")
  23. # System prompt for image description task
  24. system_prompt = "Describe the image in a few words"
  25. def format_image_prompt(context: PromptFunctionContext) -> list[dict[str, typing.Any]]:
  26. """
  27. Format the prompt for image analysis based on the AI provider.
  28. This function generates a formatted prompt for different AI providers,
  29. tailoring the structure based on the provider's requirements.
  30. Args:
  31. context (PromptFunctionContext): A dictionary containing provider information and variables.
  32. Returns:
  33. list[dict[str, typing.Any]]: A list of dictionaries representing the formatted prompt.
  34. Raises:
  35. ValueError: If an unsupported provider is specified.
  36. """
  37. if (
  38. context["provider"]["id"].startswith("bedrock:anthropic")
  39. or context["provider"]["id"] == "anthropic:claude-3-5-sonnet-20241022"
  40. ):
  41. return [
  42. {"role": "system", "content": system_prompt},
  43. {
  44. "role": "user",
  45. "content": [
  46. {
  47. "type": "image",
  48. "source": {
  49. "type": "base64",
  50. "media_type": "image/jpeg",
  51. "data": get_image_base64(context["vars"]["image_url"]),
  52. },
  53. }
  54. ],
  55. },
  56. ]
  57. # label might not exist
  58. if context["provider"].get("label") == "custom label for gpt-4.1":
  59. return [
  60. {
  61. "role": "system",
  62. "content": [{"type": "text", "text": system_prompt}],
  63. },
  64. {
  65. "role": "user",
  66. "content": [
  67. {
  68. "type": "image_url",
  69. "image_url": {
  70. "url": context["vars"]["image_url"],
  71. },
  72. }
  73. ],
  74. },
  75. ]
  76. raise ValueError(f"Unsupported provider: {context['provider']}")
Tip!

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

Comments

Loading...