SergeyO7 commited on
Commit
39f7267
·
verified ·
1 Parent(s): 6823d2e

Update prompt.txt

Browse files
Files changed (1) hide show
  1. prompt.txt +334 -10
prompt.txt CHANGED
@@ -1,18 +1,342 @@
1
  we need to add langchain control to HF AI agent code provided below.
2
- State should have location_provided boolean and location_cautions str
3
 
4
  To the existing workflow we need to add next steps:
5
- identify if location is provided in request,
 
 
6
  and if so, set location_provided true, identify if location is not on Earth,
7
  and if so,
8
  update location_cautions with "Salute you explorer!" + explain why moon indices don't work outside of Earth, and suggest principles analogous indices could be developed on other planets.
9
  final_answer = location_cautions
10
- otherwise, check if location is not fertile for planting outdoor
11
- and if so, update location_cautions so user should provide required conditions for the plant (e.g. indoor).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- otherwise, proceed with request
14
- check if request about planting or pruning?
15
- and if so,
16
- Otherwise,
17
- 1.
18
- 1.3. Pruning
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  we need to add langchain control to HF AI agent code provided below.
2
+ State should have location_provided boolean, plant str, root_crop boolean and location_cautions str, answer str
3
 
4
  To the existing workflow we need to add next steps:
5
+ 1. check if plant name is provided and recodnized (for root crop or above ground)
6
+ update plant, root_crop
7
+ 2. identify if location is provided in the request,
8
  and if so, set location_provided true, identify if location is not on Earth,
9
  and if so,
10
  update location_cautions with "Salute you explorer!" + explain why moon indices don't work outside of Earth, and suggest principles analogous indices could be developed on other planets.
11
  final_answer = location_cautions
12
+ otherwise, check if location is not fertile or sutable for planting outdoor
13
+ and if so, update location_cautions so user should ensure required conditions for the plant (e.g. indoor) before relying on the fertility indices.
14
+ otherwise, proceed with request to #2
15
+ otherwise, proceed with request to #2
16
+ 3. check if request about planting or pruning?
17
+ and for planting
18
+ check if plant not defined,
19
+ and if so, clarify from user plant and if not recognized clarify if it is crop root
20
+ update plant variable,
21
+ find out corresponding fertility index to answer
22
+ for pruning find out pruning index
23
+ 4. if location_caution <> 0 then answer = answer + location_caution
24
+
25
+ AI agent code:
26
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
27
+ import smolagents # Added for aliasing
28
+ # from smolagents.security import E2BSandbox
29
+ import datetime
30
+ import pytz
31
+ import yaml
32
+ from skyfield.api import load, Topos, load_file
33
+ from skyfield import almanac
34
+ from tools.final_answer import FinalAnswerTool
35
+ from Gradio_UI import GradioUI
36
+ import os
37
+ import base64
38
+
39
+ # Add the alias before instrumentation
40
+ smolagents.ApiModel = smolagents.HfApiModel
41
+
42
+ LANGFUSE_PUBLIC_KEY="pk-lf-133099c7-8644-49e8-8f6e-ec8bd6d543fd"
43
+ LF_SECRET_KEY = os.environ["LANGFUSE_SECRET_KEY"]
44
+ LANGFUSE_AUTH=base64.b64encode(f"{LANGFUSE_PUBLIC_KEY}:{LF_SECRET_KEY}".encode()).decode()
45
+
46
+ os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://cloud.langfuse.com/api/public/otel" # EU data region
47
+ # os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://us.cloud.langfuse.com/api/public/otel" # US data region
48
+ os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
49
+
50
+ from opentelemetry.sdk.trace import TracerProvider
51
+ from openinference.instrumentation.smolagents import SmolagentsInstrumentor
52
+ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
53
+ from opentelemetry.sdk.trace.export import SimpleSpanProcessor
54
+
55
+ trace_provider = TracerProvider()
56
+ trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
57
+
58
+ SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
59
+
60
+ # Load ephemeris and timescale
61
+ planets = load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440.bsp')
62
+ ts = load.timescale()
63
+
64
+ # Define Zodiac signs and their boundaries (0° to 360° ecliptic longitude)
65
+ ZODIAC_SIGNS = [
66
+ ("Aries", 0, 30),
67
+ ("Taurus", 30, 60),
68
+ ("Gemini", 60, 90),
69
+ ("Cancer", 90, 120),
70
+ ("Leo", 120, 150),
71
+ ("Virgo", 150, 180),
72
+ ("Libra", 180, 210),
73
+ ("Scorpio", 210, 240),
74
+ ("Sagittarius", 240, 270),
75
+ ("Capricorn", 270, 300),
76
+ ("Aquarius", 300, 330),
77
+ ("Pisces", 330, 360),
78
+ ]
79
+
80
+ # Moon phase boundaries (0° to 360° phase angle) for display purposes
81
+ MOON_PHASES = [
82
+ ("New Moon", 0, 45),
83
+ ("Waxing Crescent", 45, 90),
84
+ ("First Quarter", 90, 135),
85
+ ("Waxing Gibbous", 135, 180),
86
+ ("Full Moon", 180, 225),
87
+ ("Waning Gibbous", 225, 270),
88
+ ("Last Quarter", 270, 315),
89
+ ("Waning Crescent", 315, 360),
90
+ ]
91
+
92
+ # Fertility sign coefficients (applicable to all plants)
93
+ FERTILITY_SIGN_COEFFS = {
94
+ "Aries": 1,
95
+ "Taurus": 2,
96
+ "Gemini": 0,
97
+ "Cancer": 2,
98
+ "Leo": 1,
99
+ "Virgo": 0,
100
+ "Libra": 0.5,
101
+ "Scorpio": 1.5,
102
+ "Sagittarius": 1,
103
+ "Capricorn": 1,
104
+ "Aquarius": 0,
105
+ "Pisces": 2,
106
+ }
107
+
108
+ # Pruning sign coefficients (applicable to all plants)
109
+ PRUNING_SIGN_COEFFS = {
110
+ "Aries": 1,
111
+ "Taurus": 0,
112
+ "Gemini": 2,
113
+ "Cancer": 0,
114
+ "Leo": 1,
115
+ "Virgo": 2,
116
+ "Libra": 1.5,
117
+ "Scorpio": 0.5,
118
+ "Sagittarius": 1,
119
+ "Capricorn": 1,
120
+ "Aquarius": 2,
121
+ "Pisces": 0,
122
+ }
123
+
124
+ # Fertility phase coefficients for above-ground plants
125
+ FERTILITY_PHASE_COEFFS_ABOVE = {
126
+ "New Moon": 0,
127
+ "Waxing Moon": 1,
128
+ "Full Moon": 0,
129
+ "Waning Moon": 0.5,
130
+ }
131
+
132
+ # Fertility phase coefficients for root crops
133
+ FERTILITY_PHASE_COEFFS_ROOT = {
134
+ "New Moon": 0,
135
+ "Waxing Moon": 0.5,
136
+ "Full Moon": 0,
137
+ "Waning Moon": 1,
138
+ }
139
+
140
+ # Pruning phase coefficients
141
+ PRUNING_PHASE_COEFFS = {
142
+ "New Moon": 0,
143
+ "Waxing Moon": 1,
144
+ "Full Moon": 0,
145
+ "Waning Moon": 0.5,
146
+ }
147
+
148
+ @tool
149
+ def get_moon_info(date_time: str) -> dict:
150
+ """
151
+ Returns Moon's Zodiac position, phase, and fertility and pruning indices for the given date/time.
152
 
153
+ The fertility and pruning indices are calculated as sum of sign and phase fertility values of the Moon position. Moon sign fertility
154
+ amounts up to 2.0 value and phase fertility value could be 1.0 max.
155
+ It is observed that when Moon is in different Zodiac signs, the fertility of new plants and impact of pruning differs.
156
+ When Moon is in fertile sign the plant is in the active phase, when all processes are particularly intense, and any intervention
157
+ such as pruning can be very traumatic for the plant. Here:
158
+ Most fertile signs: Taurus, Pisces, Cancer - Plants are in the active growth phase, juices and nutrients actively circulate
159
+ in the plant, and it is best time for fertilizers, harvasting cutting, vaccination, rooting.
160
+ Conditionally fertile: Scorpio
161
+ Neutral: Aries, Leo, Sagittarius, Capricorn
162
+ Conditionally sterile: Libra
163
+ Sterile: Gemini, Virgo, Aquarius
164
+
165
+ Fertility indices ranges from 0.0 to 3.0 where proportionaly
166
+ 0 - minimal expected fertility
167
+ 3.0 - most favorable fertility for platining,
168
+ and depends on type of plant (root crop or produce above ground).
169
+
170
+ Pruning indices ranges from 0 to 3 where proportionaly:
171
+ 0 - pruning is not recommended as it causes most damage to tree and can lead to:
172
+ Increased sap production from the cut points
173
+ Increased vulnerability to infections
174
+ Delayed wound healing
175
+ Possible weakening of the plant.
176
+ Instead of pruning into fertile signs, you can do:
177
+ Crown formation
178
+ Pinching the shoots
179
+ Removing dead branches
180
+ Sanitary treatment
181
+ 1.0 - pruning is not recommended,
182
+ 2.0 - allowed only minimum or sanitary pruning,
183
+ 3.0 - most favorable time for pruning.
184
+ Args:
185
+ date_time (str): ISO 8601 formatted datetime (YYYY-MM-DDTHH:MM:SS)
186
+ Returns:
187
+ dict: {
188
+ "zodiac_position": "Leo 15°30'",
189
+ "moon_phase": "Waxing Gibbous",
190
+ "fertility_above_ground": 2.0,
191
+ "fertility_root_crop": 1.5,
192
+ "pruning": 2.0
193
+ }
194
+ """
195
+ try:
196
+ # Parse input datetime and localize to UTC
197
+ user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
198
+ user_time = pytz.utc.localize(user_time)
199
+
200
+ # Use loaded ephemeris and timescale
201
+ t = ts.from_datetime(user_time)
202
+
203
+ # Define celestial bodies
204
+ earth = planets['earth']
205
+ moon = planets['moon']
206
+ sun = planets['sun']
207
+
208
+ # Calculate Moon's ecliptic longitude
209
+ astrometric = earth.at(t).observe(moon)
210
+ ecliptic_lat, ecliptic_lon, distance = astrometric.ecliptic_latlon()
211
+ lon_deg = ecliptic_lon.degrees % 360
212
+
213
+ # Calculate the phase angle using almanac.moon_phase
214
+ phase = almanac.moon_phase(planets, t)
215
+ phase_angle = phase.degrees
216
+
217
+ # Determine Zodiac sign and position
218
+ zodiac_sign = "Unknown"
219
+ position_degrees = 0
220
+ for sign, start, end in ZODIAC_SIGNS:
221
+ if start <= lon_deg < end:
222
+ zodiac_sign = sign
223
+ position_degrees = lon_deg - start
224
+ break
225
+
226
+ # Format position to degrees and minutes
227
+ degrees = int(position_degrees)
228
+ minutes = int((position_degrees % 1) * 60)
229
+ position_str = f"{zodiac_sign} {degrees}°{minutes:02}'"
230
+
231
+ # Determine moon phase for display
232
+ moon_phase = "Unknown"
233
+ for phase, start, end in MOON_PHASES:
234
+ if start <= phase_angle < end:
235
+ moon_phase = phase
236
+ break
237
+
238
+ # Determine phase category for indices with 15° orbis for New and Full Moon
239
+ if (phase_angle >= 345 or phase_angle < 15):
240
+ phase_category = "New Moon" # 345° to 15° (30° total orbis)
241
+ elif 15 <= phase_angle < 165:
242
+ phase_category = "Waxing Moon"
243
+ elif 165 <= phase_angle < 195:
244
+ phase_category = "Full Moon" # 165° to 195° (30° total orbis)
245
+ elif 195 <= phase_angle < 345:
246
+ phase_category = "Waning Moon"
247
+ else:
248
+ phase_category = "Unknown"
249
+
250
+ # Calculate fertility and pruning indices
251
+ if zodiac_sign in FERTILITY_SIGN_COEFFS and phase_category in FERTILITY_PHASE_COEFFS_ABOVE:
252
+ fertility_above_ground = FERTILITY_SIGN_COEFFS[zodiac_sign] + FERTILITY_PHASE_COEFFS_ABOVE[phase_category]
253
+ fertility_root_crop = FERTILITY_SIGN_COEFFS[zodiac_sign] + FERTILITY_PHASE_COEFFS_ROOT[phase_category]
254
+ pruning = PRUNING_SIGN_COEFFS[zodiac_sign] + PRUNING_PHASE_COEFFS[phase_category]
255
+ else:
256
+ fertility_above_ground = None
257
+ fertility_root_crop = None
258
+ pruning = None
259
+
260
+ return {
261
+ "zodiac_position": position_str,
262
+ "moon_phase": moon_phase,
263
+ "fertility_above_ground": fertility_above_ground,
264
+ "fertility_root_crop": fertility_root_crop,
265
+ "pruning": pruning
266
+ }
267
+
268
+ except Exception as e:
269
+ raise ValueError(f"Error in get_moon_info: {str(e)}")
270
+
271
+ @tool
272
+ def get_current_time_in_timezone(timezone: str) -> str:
273
+ """
274
+ Returns the current local time in the specified timezone with description.
275
+ Args:
276
+ timezone (str): A string representing a valid timezone (e.g., 'UTC')
277
+ Returns:
278
+ str: Formatted local time with timezone description
279
+ """
280
+ try:
281
+ tz = pytz.timezone(timezone)
282
+ now = datetime.datetime.now(tz)
283
+ return f"Local time in {timezone}: {now.strftime('%Y-%m-%d %H:%M:%S')}"
284
+ except Exception as e:
285
+ return f"Error: {str(e)}"
286
+
287
+ @tool
288
+ def get_current_time_raw(timezone: str) -> str:
289
+ """
290
+ Returns current local time in specified timezone as ISO 8601 string.
291
+ Args:
292
+ timezone (str): A string representing a valid timezone (e.g., 'UTC')
293
+ Returns:
294
+ str: Datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
295
+ """
296
+ try:
297
+ tz = pytz.timezone(timezone)
298
+ now = datetime.datetime.now(tz)
299
+ return now.strftime("%Y-%m-%dT%H:%M:%S")
300
+ except Exception as e:
301
+ return f"Error: {str(e)}"
302
+
303
+ # Model configuration
304
+ final_answer = FinalAnswerTool()
305
+ model = HfApiModel(
306
+ max_tokens=2096,
307
+ temperature=0.5,
308
+ model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/",
309
+ custom_role_conversions=None,
310
+ )
311
+
312
+ # Load image tool from Hub
313
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
314
+
315
+ # Load prompt templates
316
+ with open("prompts.yaml", 'r') as stream:
317
+ prompt_templates = yaml.safe_load(stream)
318
+
319
+ # Initialize agent with all tools
320
+ agent = CodeAgent(
321
+ model=model,
322
+ tools=[final_answer, get_moon_info, get_current_time_in_timezone, get_current_time_raw, image_generation_tool],
323
+ max_steps=6,
324
+ verbosity_level=1,
325
+ prompt_templates=prompt_templates
326
+ # execution_env=E2BSandbox(
327
+ # allowed_imports=["numpy", "pandas"], # Explicitly permitted packages
328
+ # blocked_imports=["subprocess"], # Prevent system access
329
+ # ),
330
+ # safe_mode=True, # Enable safe code execution
331
+ # timeout=10, # Seconds before execution timeout
332
+ # max_memory=512, # MB memory limit
333
+ # file_system_access=False, # Disable disk write access
334
+ # network_access=False, # Block network operations
335
+ # max_code_iterations=100, # Prevent infinite loops
336
+ )
337
+
338
+ if __name__ == "__main__":
339
+ GradioUI(agent).launch()
340
+
341
+ # Change to your username and repo name
342
+ # agent.push_to_hub('sergeyo7/Garden_Magus')