AI actually doing muliple steps 🫢

This commit is contained in:
2025-09-09 10:35:48 +03:00
parent c3e2ebada0
commit ee42034333

52
main.py
View File

@@ -34,20 +34,11 @@ You do not need to specify the working directory in your function calls
as it is automatically injected for security reasons. as it is automatically injected for security reasons.
""" """
config=types.GenerateContentConfig(
tools=[available_functions], system_instruction=system_prompt
)
messages = []
load_dotenv() load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY") api_key = os.environ.get("GEMINI_API_KEY")
client = genai.Client(api_key=api_key) client = genai.Client(api_key=api_key)
def add_message(user, message):
messages.append({"role": user, "parts": [{"text": message}]})
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Error: No prompt provided.\nUsage: uv run main.py \"<your prompt here>\" [--verbose]") print("Error: No prompt provided.\nUsage: uv run main.py \"<your prompt here>\" [--verbose]")
@@ -65,29 +56,44 @@ def main():
if verbose: if verbose:
print(f'User prompt: "{user_prompt}"') print(f'User prompt: "{user_prompt}"')
add_message("user", user_prompt) messages = [
types.Content(role="user", parts=[types.Part.from_text(text=user_prompt)]),
]
for iteration in range(20):
response = client.models.generate_content( response = client.models.generate_content(
model="gemini-2.0-flash-001", model="gemini-2.0-flash-001",
contents=messages, contents=messages,
config=config, config=types.GenerateContentConfig(
tools=[available_functions],
system_instruction=system_prompt,
),
) )
if response.candidates[0].content.parts: if not response.candidates:
for part in response.candidates[0].content.parts: print("No response, stopping.")
break
# String representation of final text (if the model is done)
candidate = response.candidates[0]
messages.append(candidate.content)
has_function_call = False
final_texts = []
for part in candidate.content.parts:
if part.function_call: if part.function_call:
has_function_call = True
function_result = call_function(part.function_call, verbose=verbose) function_result = call_function(part.function_call, verbose=verbose)
# validate that function call produced a response messages.append(function_result)
if not (
function_result.parts
and function_result.parts[0].function_response
and function_result.parts[0].function_response.response
):
raise RuntimeError("Fatal: Function call returned no response.")
elif part.text: elif part.text:
reply_text = part.text final_texts.append(part.text)
add_message("model", reply_text)
print(reply_text) # Only finish if there was NO tool call in this iteration
if not has_function_call and final_texts:
print("Final response:")
print("\n".join(final_texts))
break
if verbose: if verbose:
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}") print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")