Skip to content

Commit

Permalink
Cleaned up code and added repair.
Browse files Browse the repository at this point in the history
  • Loading branch information
saj122 committed Sep 27, 2023
1 parent e471f8d commit f800d3b
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 544 deletions.
177 changes: 166 additions & 11 deletions Src/Kernel/FormulaPy/formula.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,203 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "f1ef8032-ecf3-46f0-9af2-564c8e047e29",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully initialized formula.\n"
]
}
],
"source": [
"%load_ext formula"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "6b368acc-9353-45e3-8e52-7fe54c5b087f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Compiled) InvalidBatteryChecker.4ml\r\n",
"0.59s.\r\n",
"[]> \n"
]
}
],
"source": [
"%load /home/stephen/git/formula/Tst/Tests/Symbolic/MappingExample.4ml"
"%load C:\\Users\\sajtt\\Documents\\InvalidBatteryChecker.4ml"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "cbb40c73-5c52-42fa-9786-a07d501786d3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Parsing text took: 0\r\n",
"Visiting text took: 0\r\n",
"Started solve task with Id 0.\r\n",
"0.32s.\r\n",
"[]> \n"
]
}
],
"source": [
"%solve invalidBatteryPartialModel 1 BatteryChecker.conforms"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "32107a2e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model not solvable. No unsat core terms generated.\r\n",
"0.00s.\r\n",
"[]> \n"
]
}
],
"source": [
"%extract 0 0 0"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b3fed9f2",
"metadata": {},
"outputs": [],
"source": [
"%solve pm 1 Mapping.conforms"
"explain_prompt = \"Given the following Formula DSL program modeling a physical drone,\\\n",
" explain what the invalidBatteryPartialModel partial model is modeling \\\n",
" and give detailed solutions that would decrease the battery consumption in the invalidBatteryPartialModel.\\\n",
" Put the solutions in a list.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "aaecf243-7534-4355-908e-375fd02db84f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running executor...\n",
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Failed to post https://api.smith.langchain.com/runs in LangSmith API. 401 Client Error: Unauthorized for url: https://api.smith.langchain.com/runs\n",
"{\"detail\":\"Invalid auth\"}\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32;1m\u001b[1;3mThe invalidBatteryPartialModel is modeling a drone mission with a battery that has an unknown capacity (represented by the variable x). The model includes the components of the drone (payload and body) and the locations for each mission item. It also includes the mission items themselves, which consist of a label, source and destination locations, distance, and velocity.\n",
"\n",
"The problem with the invalidBatteryPartialModel is that it does not specify a valid battery capacity. This makes it impossible to calculate the energy consumption and check if the battery capacity is exceeded. To fix this, we need to provide a valid battery capacity value.\n",
"\n",
"Solutions to decrease battery consumption in the invalidBatteryPartialModel:\n",
"1. Optimize the drone's flight path: By optimizing the flight path, the drone can minimize the distance traveled and reduce the overall energy consumption.\n",
"2. Reduce the payload weight: The payload weight directly affects the energy consumption of the drone. By reducing the payload weight, the battery consumption can be decreased.\n",
"3. Improve the drone's efficiency: This can be achieved by using more efficient components and technologies, such as lightweight materials, efficient motors, and aerodynamic designs.\n",
"4. Adjust the drone's velocity: By adjusting the drone's velocity based on the mission requirements, the energy consumption can be optimized. Slower velocities generally result in lower energy consumption.\n",
"5. Implement energy-saving algorithms: Use algorithms that optimize the drone's flight path and energy consumption, taking into account factors such as wind conditions, altitude, and mission requirements.\n",
"6. Increase the battery capacity: If the current battery capacity is insufficient for the mission, consider upgrading to a higher capacity battery to ensure sufficient power for the entire mission.\n",
"\n",
"Note: The specific solutions may vary depending on the requirements and constraints of the drone mission.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
}
],
"source": [
"%explain --explain-prompt \"$explain_prompt\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "ac31c0b2",
"metadata": {},
"outputs": [],
"source": [
"%explain"
"repair_prompt = \"Repair the invalidBatteryPartialModel by reducing the payload1 and body component weights \\\n",
" so that the sum of the missionConsumptions will be below the batteryCapacity. Edit the code and enclose in triple backticks.\""
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "50f00b79",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running repair...\n",
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3mThe invalidBatteryPartialModel is not solvable because the sum of the missionConsumptions exceeds the batteryCapacity. To fix this, we need to reduce the weights of the payload1 and body components so that the sum of the missionConsumptions will be below the batteryCapacity.\n",
"\n",
"Here is the corrected code:\n",
"\n",
"```formula\n",
"partial model invalidBatteryPartialModel of BatteryChecker\n",
"[solver_RecursionBound = 200]\n",
"{\n",
" Component(\"payload1\", 5). // Reduce the weight of payload1 to 5\n",
" Component(\"body\", 5). // Reduce the weight of body to 5\n",
" Battery(\"battery1\", 10, x).\n",
"\n",
" // Rest of the code remains the same\n",
" ...\n",
"}\n",
"```\n",
"\n",
"By reducing the weights of the payload1 and body components to 5, the sum of the missionConsumptions will be below the batteryCapacity and the model will be solvable.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
}
],
"source": [
"%repair --repair-prompt \"$repair_prompt\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a72c0a0-e898-4f08-9737-147725f20987",
"id": "031a42f0",
"metadata": {},
"outputs": [],
"source": []
Expand All @@ -65,7 +220,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion Src/Kernel/FormulaPy/formula/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
load("coreclr")
import clr
d = __file__.replace("__init__.py","")
clr.AddReference(d + "CommandLine/CommandLine.dll")
clr.AddReference(d + "CommandLine/VUISIS.Formula.x64.dll")

from .formula_magic import FormulaMagics

Expand Down
57 changes: 40 additions & 17 deletions Src/Kernel/FormulaPy/formula/formula_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain.schema.messages import SystemMessage
from langchain.memory import ConversationBufferMemory
from .formula_tools import DebugFormulaCodeLLM, LoadFormulaCode, QueryFormulaCode, DecodeFormulaCodeLLM
from .formula_tools import FormulaCodeLLM, RepairFormulaCodeLLM
from .config import cfg
from .prompts import FIX_CODE_PREFIX, QUERY_PROMPT
from .prompts import FIX_CODE_PREFIX, REPAIR_CODE_PREFIX

os.environ["OPENAI_API_KEY"] = cfg["OPENAI_API_KEY"]
#os.environ["OPENAI_API_TYPE"] = "azure"
#os.environ["OPENAI_API_VERSION"] = "2023-08-24"
#os.environ["OPENAI_API_BASE"] = "https://apim.app.vanderbilt.edu/openai-students/deployments/gpt-35-turbo/chat/completions?api-version=2023-08-24"



if cfg["LANGCHAIN_API_KEY"] != "":
Expand All @@ -18,32 +22,51 @@
os.environ["LANGCHAIN_PROJECT"] ="pt-oily-sultan-99"


llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k")
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")


system_message = SystemMessage(content=FIX_CODE_PREFIX)
_prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

tools = [LoadFormulaCode, QueryFormulaCode, DecodeFormulaCodeLLM(llm=llm), DebugFormulaCodeLLM(llm=llm)]
repair_system_message = SystemMessage(content=REPAIR_CODE_PREFIX)
_repair_prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)

explain_tools = [FormulaCodeLLM(llm=llm)]

agent = OpenAIFunctionsAgent(
llm=llm,
prompt=_prompt,
tools=tools,
memory=memory,
tools=explain_tools,
verbose=True
)
)

agent_executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
tools=explain_tools,
verbose=True,
)

def run_agent_executor(code, output, additional_details):
query = QUERY_PROMPT.format(code=code, interpreter_output=output)
if additional_details:
query += f"\n\nHere are some additional details to keep in mind when trying to figure \
out what is wrong with the code:\n\n{additional_details}"
agent_executor.run(query)
)

repair_tools = [RepairFormulaCodeLLM(llm=llm)]

agent_repair = OpenAIFunctionsAgent(
llm=llm,
prompt=_repair_prompt,
tools=repair_tools,
verbose=True
)

agent_executor_repair = AgentExecutor.from_agent_and_tools(
agent=agent_repair,
tools=repair_tools,
verbose=True,
)

def run_agent_executor(code, output, explain_prompt):
prompts = "{explain_prompt}\n\n{code}\n\n{output}".format(code=code, output=output, explain_prompt=explain_prompt)
prompts.encode('unicode_escape')
agent_executor.run(prompts)

def run_agent_executor_repair(code, output, repair_prompt):
prompts = "{repair_prompt}\n\n{code}\n\n{output}".format(code=code, output=output, repair_prompt=repair_prompt)
prompts.encode('unicode_escape')
agent_executor_repair.run(prompts)
Loading

0 comments on commit f800d3b

Please sign in to comment.