Skip to content

Commit

Permalink
Merge pull request #27 from MarketSquare/25-library-cannot-be-load
Browse files Browse the repository at this point in the history
fix F strings, removed selenium lib from req, debug logs
  • Loading branch information
xfoggi authored Oct 19, 2024
2 parents 020e703 + adbf836 commit ce4011e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Documentation Tests the functionality of the generate test data keyword
Library Collections
Library ../../src/RobotFrameworkAI/modules/real_test_data_generator/RealTestDataGenerator.py
Library ../../src/RobotFrameworkAI/logger/logger.py


*** Variables ***
Expand All @@ -16,41 +17,41 @@ ${TOP_P} 1
${FREQUENCY_PENALTY} 0
${PRESENCE_PENALTY} 0
${COUNTRY} Czech Republic
${GENERATED_TEST_DATA} ${EMPTY}


*** Test Cases ***
Acceptance Test: Generate Test Data
[Documentation] Verify the behavior of generate_test_data keyword with various input arguments
[Tags] acceptance
When Generate Test Data
Then Test Data Is Generated Successfully
And Test Data Is In Right Format ${AMOUNT}
Setup Logging
Set AI Model ${AI_MODEL}
Set Type ${TYPE}
Set Model ${MODEL}
Set Amount ${AMOUNT}
Set Format ${FORMAT}
Set Max Tokens ${MAX_TOKENS}
Set Temperature ${TEMPERATURE}
Set Top P ${TOP_P}
Set Frequency Penalty ${FREQUENCY_PENALTY}
Set Presence Penalty ${PRESENCE_PENALTY}

${test_data}= When Generate Test Data
Then Test Data Is Generated Successfully ${test_data}
And Test Data Is In Right Format ${test_data} ${AMOUNT}


*** Keywords ***
When Generate Test Data
[Documentation] Generate test data
${test_data} = Generate Test Data
... ${AI_MODEL}
... ${TYPE}
... ${MODEL}
... ${AMOUNT}
... ${FORMAT}
... ${MAX_TOKENS}
... ${TEMPERATURE}
... ${TOP_P}
... ${FREQUENCY_PENALTY}
... ${PRESENCE_PENALTY}
... country=${COUNTRY}
VAR ${GENERATED_TEST_DATA} ${test_data} scope=test
${test_data} = Generate Test Data country=${COUNTRY}
RETURN ${test_data}

Then Test Data Is Generated Successfully
[Documentation] Check if keyword returned a non-empty list
Should Not Be Empty ${GENERATED_TEST_DATA}
[Arguments] ${test_data}
Should Not Be Empty ${test_data}

And Test Data Is In Right Format
[Documentation] Check if the list has the right amount of elements
[Arguments] ${expected_amount}
${actual_amount} = Get Length ${GENERATED_TEST_DATA}
[Arguments] ${test_data} ${expected_amount}
${actual_amount} = Get Length ${test_data}
Should Be Equal As Numbers ${actual_amount} ${expected_amount}
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
robotframework
robotframework-seleniumlibrary
robotframework-pythonlibcore
openai
pytest
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from openai import OpenAI
from typing import Optional

from RobotFrameworkAI.ai_interface.ai_model_services.AIModelStrategy import AIModelStrategy
from RobotFrameworkAI.objects.response.Response import Response
Expand All @@ -20,13 +21,21 @@
class OpenAIService(AIModelStrategy):
"""
The class in charge of handling communication with the OpenAI API
This class is an implementation of the abstract class AIModelStrategy.
Prompts directed at the OpenAI API, will get send to the right OpenAI AI tool.
Prompts directed at the OpenAI API will get sent to the right OpenAI AI tool.
All the logic doing that can be found in the abstract class AIModelStrategy.
"""
def __init__(self) -> None:

def __init__(self, openai_key: Optional[str] = None) -> None:
super().__init__()
self.name = "openai"
client = OpenAI(api_key=os.environ["OPENAI_KEY"])

# Use the provided key or fallback to the environment variable
self.openai_key = openai_key or os.getenv("OPENAI_KEY")
if not self.openai_key:
raise ValueError("OpenAI API key must be provided either as a parameter or via the OPENAI_KEY environment variable.")

print("OpenAI API key:", self.openai_key)
client = OpenAI(api_key=self.openai_key)
self.ai_tools = self._discover_tools("openai_tools", OpenAITool, client)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from RobotFrameworkAI.ai_interface.ai_model_tools.AssistantTool import AssistantTool
from RobotFrameworkAI.objects.response.Response import Response
from RobotFrameworkAI.objects.response.ResponseMetadata import ResponseMetadata
import logging

logger = logging.getLogger(__name__)

class OpenAIAssistant(OpenAITool, AssistantTool):
"""
Expand Down Expand Up @@ -52,10 +54,9 @@ def update_assistant(self, prompt):
# Remove empty strings from list
updated_parameters = [updated_parameter for updated_parameter in updated_parameters if updated_parameter]

updated_parameters = updated_parameters if updated_parameters else ["no parameters have been updated."]
message = f"Succesfully updated assistant `{self.assistant.name}` with id `{self.assistant.id}`. Updated parameters: {", ".join(updated_parameters)}."
updated_parameters = updated_parameters if updated_parameters else ["no parameters have been updated."]
message = f"Successfully updated assistant `{self.assistant.name}` with id `{self.assistant.id}`. Updated parameters: {', '.join(updated_parameters)}."


self.assistant = self.client.beta.assistants.update(
self.assistant.id,
name=assistant_data.name,
Expand All @@ -70,6 +71,7 @@ def update_assistant(self, prompt):
response = Response(message, response_metadata)
return response


def get_active_assistant_id(self, _ = None):
response_metadata = ResponseMetadata(self.tool_name, self.ai_model_name, self.assistant.model)
response = Response(self.assistant.id, response_metadata)
Expand Down Expand Up @@ -111,7 +113,7 @@ def attach_files(self, prompt):
tool_resources = {"file_search": {"vector_store_ids": [vector_store.id]}},
)
response_metadata = ResponseMetadata(self.tool_name, self.ai_model_name, self.assistant.model)
message = f"Succesfully added {len(files)} files to assistant with id `{self.assistant.id}` and name `{self.assistant.name}`. The following files got added: `{"`, `".join([file[0] for file in files])}`"
message = f"Succesfully added {len(files)} files to assistant with id `{self.assistant.id}` and name `{self.assistant.name}`. The following files got added: `{'`, `'.join([file[0] for file in files])}`"
response = Response(message, response_metadata)
return response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def collect_files_from_folder(self, folder_path):
if self.has_allowed_extension(file):
full_path = os.path.join(root, file)
file_paths.append(full_path)
logger.debug(f"Found the following files in `{folder_path}`: `{"`, `".join(file_paths)}`")
logger.debug(f"Found the following files in `{folder_path}`: `{'`, `'.join(file_paths)}`")
return file_paths

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,9 @@ def generate_test_data(
model, max_tokens, temperature, top_p, frequency_penalty, presence_penalty, response_format.
Each argument has its own setter, the name of the keyword is 'set' plus the name of the argument e.g. Set AI Model for AI Model.
"""
"""
# If arguments are not given directly, get its default value. This is the value of the class attribute with the same name
ai_model, model, max_tokens, temperature, top_p, frequency_penalty, presence_penalty, \
type, amount, format, kwargs = self.get_default_values_for_arguments(
ai_model, model, max_tokens, temperature, top_p, frequency_penalty, presence_penalty, type, amount, format, kwargs = self.get_default_values_for_arguments(
ai_model=ai_model,
model=model,
max_tokens=max_tokens,
Expand Down Expand Up @@ -199,8 +198,18 @@ def generate_test_data(
presence_penalty,
response_format
)
response = self.ai_interface.call_ai_tool(prompt)
response = generator.format_response(response)
logger.debug(f"Prompt: {prompt}")
try:
response = self.ai_interface.call_ai_tool(prompt)
logger.debug(f"Response from AI tool: {response}")
response = generator.format_response(response)
logger.debug(f"Formatted response: {response}")
except Exception as e:
error_message = f"Failed to generate test data: {e}"
logger.error(error_message)
raise ValueError(error_message)

logger.debug(f"Generated test data: {response}")
return response

# Validation methods
Expand Down
2 changes: 1 addition & 1 deletion utest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import logging
from src.RobotFrameworkAI.logger.logger import setup_logging
from RobotFrameworkAI.logger.logger import setup_logging

logger = logging.getLogger(__name__)

Expand Down

0 comments on commit ce4011e

Please sign in to comment.