-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathagent_helper_function.py
More file actions
201 lines (164 loc) · 6.75 KB
/
agent_helper_function.py
File metadata and controls
201 lines (164 loc) · 6.75 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import json
import asyncio
from typing import Sequence
from autogen_agentchat.ui import Console
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import SelectorGroupChat
from autogen_agentchat.messages import AgentEvent, ChatMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_core.memory import ListMemory, MemoryContent, MemoryMimeType
from autogen_agentchat.conditions import TextMentionTermination
from autogen_ext.agents.web_surfer import MultimodalWebSurfer
from pyomo.environ import (
Constraint,
Var,
ConcreteModel,
Expression,
Objective,
SolverFactory,
TransformationFactory,
value,
)
from pyomo.network import Arc, SequentialDecomposition
from idaes.core import FlowsheetBlock
from idaes.models.unit_models import (
PressureChanger,
Mixer,
Separator as Splitter,
Heater,
CSTR,
)
from idaes.models.unit_models import Flash
from idaes.models.unit_models.pressure_changer import ThermodynamicAssumption
from idaes.core.util.model_statistics import degrees_of_freedom
import idaes.logger as idaeslog
from idaes.core.solvers import get_solver
from idaes.core.util.exceptions import InitializationError
from idaes_examples.mod.hda import hda_ideal_VLE as thermo_props
from idaes_examples.mod.hda import hda_reaction as reaction_props
import logging
from hda_objective_function import hda_objective
import time
import tiktoken
constraint_memory = None
validator_memory = None
llm_config = None
param_history = []
# Tool Functions
async def calculate_params_tool(conditions: dict, metric: str) -> tuple:
"""Calculates the objective metric for the HDA process based on given operating conditions.
Args:
conditions (dict): The current parameter values. Example:
{
"H101_temperature": float,
"F101_temperature": float,
"F102_temperature": float,
"F102_deltaP": float
}
metric: given objective metric
Returns:
- metric (str): The objective metric that the function is calculating
"""
await asyncio.sleep(1.5)
value = hda_objective(
conditions['H101_temperature'],
conditions['F101_temperature'],
conditions['F102_temperature'],
conditions['F102_deltaP'],
metric
)
if isinstance(value, str):
value = 'Invalid Conditions'
if conditions not in param_history:
param_history.append(conditions.copy())
await add_suggestion_memory(conditions, metric, value)
return value
async def validate(
vals: dict,
changes: dict,
constraints: dict
) -> dict:
"""
Applies changes to the current parameter values and validates the result.
This function apply a list of proposed adjustments to the current values dictionary
then validate the resulting updated values against the global constraints.
Only returns updated values if the proposed changes result in a valid configuration.
Args:
vals (dict): The current parameter values. Example:
{
"H101_temperature": float,
"F101_temperature": float,
"F102_temperature": float,
"F102_deltaP": float
}
changes (dict): A dictionary of changes of each parameter, with keys being the parameter
and the coorsponding values being the numeric change. Example:
{
"H101_temperature": -5.0,
"F101_temperature": -10.0,
"F102_temperature": 15.0,
"F102_deltaP": -20000
}
constraints (dict): Dictionary defining valid ranges. Example:
{
"H101_temperature": [[<lower>, <upper>],
"F101_temperature": [<lower>, <upper>],
"F102_temperature": [<lower>, <upper>],
"F102_deltaP": [<lower>, <upper>]
}
Returns:
dict: A dictionary containing:
- "result" (str):
- "All Valid" if the updated values pass validation,
- Otherwise a description of the validation failure.
- "conditions" (dict):
- The updated values if valid,
- Otherwise the original `vals` unchanged.
"""
updated_vals = vals.copy()
for p in changes:
updated_vals[p] += changes[p]
# Validation logic
if updated_vals in param_history:
return {"result": "Invalid, this set of value is repeated", "values": vals}
if updated_vals['H101_temperature'] < constraints['H101_temperature'][0] or updated_vals['H101_temperature'] > constraints['H101_temperature'][1]:
return {
"result": f"Invalid, H101_temperature should be within constraint {constraints['H101_temperature']}",
"conditions": vals
}
if updated_vals['F101_temperature'] < constraints['F101_temperature'][0] or updated_vals['F101_temperature'] > constraints['F101_temperature'][1]:
return {
"result": f"Invalid, F101_temperature should be within constraint {constraints['F101_temperature']}",
"conditions": vals
}
if updated_vals['F102_temperature'] < constraints['F102_temperature'][0] or updated_vals['F102_temperature'] > constraints['F102_temperature'][1]:
return {
"result": f"Invalid, F102_temperature should be within constraint {constraints['F102_temperature']}",
"conditions": vals
}
if updated_vals['F102_deltaP'] < constraints['F102_deltaP'][0] or updated_vals['F102_deltaP'] > constraints['F102_deltaP'][1]:
return {
"result": f"Invalid, F102_deltaP should be within constraint {constraints['F102_deltaP']}",
"conditions": vals
}
return {
"result": "All Valid",
"conditions": updated_vals
}
async def add_suggestion_memory(
conditions: dict,
metric: str,
value: float
):
current_memory = constraint_memory.content
encoding = tiktoken.encoding_for_model(llm_config["model"])
total_tokens = sum(len(encoding.encode(m.content)) for m in current_memory)
if total_tokens > llm_config["model_info"]["max_tokens"]*0.9:
current_memory.pop(1)
constraint_memory.content = current_memory
await constraint_memory.add(MemoryContent(
content=f"H101_temperature:{conditions['H101_temperature']}, F101_temperature: {conditions['F101_temperature']}, F102_temperature: {conditions['F102_temperature']}, F102_deltaP: {conditions['F102_deltaP']}, leads to {metric} = {value}.",
mime_type=MemoryMimeType.TEXT
))
async def add_context(memory, content):
await memory.add(MemoryContent(content=content, mime_type=MemoryMimeType.TEXT))