From e5b57f985c113184ff8fd92390de279ae0347335 Mon Sep 17 00:00:00 2001 From: evalstate <1936278+evalstate@users.noreply.github.com.> Date: Wed, 24 Jul 2024 16:33:49 +0200 Subject: [PATCH] Added ability to configure temperature. The following Claude Engineer prompt was used for this change: ``` update main.py to allow the user to configure the model temperature by setting a TEMPERATURE variable in the .env file. if not set or invalid then the API default should be used - do NOT set the parameter on API calls in this case. the value is set in the client.messages.create function. prefer dictionary unpacking with comprehension to set the value in the API call (only if it has been supplied) to keep the code changes simple and minimal. maintain consistency with existing environment variable handling and code. if the user has set a TEMPERATURE variable, then display the setting after the existing welcome message. ``` --- .env.example | 1 + main.py | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 83052419..a563dd4d 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ ANTHROPIC_API_KEY="YOUR API KEY" TAVILY_API_KEY="YOUR API KEY" +#TEMPERATURE=Value between 0.0 and 1.0 \ No newline at end of file diff --git a/main.py b/main.py index b1307837..6c65ee2c 100644 --- a/main.py +++ b/main.py @@ -60,6 +60,16 @@ def setup_virtual_environment() -> Tuple[str, str]: anthropic_api_key = os.getenv("ANTHROPIC_API_KEY") if not anthropic_api_key: raise ValueError("ANTHROPIC_API_KEY not found in environment variables") + +# Read TEMPERATURE from environment variables +temperature = os.getenv("TEMPERATURE") +if temperature is not None: + try: + temperature = float(temperature) + except ValueError: + print("Invalid TEMPERATURE value in .env file. Using API default.") + temperature = None + client = Anthropic(api_key=anthropic_api_key) # Initialize the Tavily client @@ -362,7 +372,8 @@ async def generate_edit_instructions(file_path, file_content, instructions, proj extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"}, messages=[ {"role": "user", "content": "Generate SEARCH/REPLACE blocks for the necessary changes."} - ] + ], + **({'temperature': temperature} if temperature is not None else {}) ) # Update token usage for code editor code_editor_tokens['input'] += response.usage.input_tokens @@ -861,7 +872,8 @@ async def send_to_ai_for_executing(code, execution_result): system=system_prompt, messages=[ {"role": "user", "content": f"Analyze this code execution from the 'code_execution_env' virtual environment:\n\nCode:\n{code}\n\nExecution Result:\n{execution_result}"} - ] + ], + **({'temperature': temperature} if temperature is not None else {}) ) # Update token usage for code execution @@ -977,7 +989,8 @@ async def chat_with_claude(user_input, image_path=None, current_iteration=None, extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"}, messages=messages, tools=tools, - tool_choice={"type": "auto"} + tool_choice={"type": "auto"}, + **({'temperature': temperature} if temperature is not None else {}) ) # Update token usage for MAINMODEL main_model_tokens['input'] += response.usage.input_tokens @@ -1074,7 +1087,8 @@ async def chat_with_claude(user_input, image_path=None, current_iteration=None, extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"}, messages=messages, tools=tools, - tool_choice={"type": "auto"} + tool_choice={"type": "auto"}, + **({'temperature': temperature} if temperature is not None else {}) ) # Update token usage for tool checker tool_checker_tokens['input'] += tool_response.usage.input_tokens @@ -1202,6 +1216,8 @@ async def main(): console.print("Type 'reset' to clear the conversation history.") console.print("Type 'save chat' to save the conversation to a Markdown file.") console.print("While in automode, press Ctrl+C at any time to exit the automode to return to regular chat.") + if temperature is not None: + console.print(f"Model temperature set to: {temperature}") while True: user_input = await get_user_input()