Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: misc issues with prediction tool #121

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions tools/optimization_by_prompting.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ def create_new_instructions(llm, instructions, score):
return evaluations


def prompt_engineer(init_instructions, instructions_format, iterations=3, model_name="gpt-3.5-turbo"):
def prompt_engineer(openai_api_key, init_instructions, instructions_format, iterations=3, model_name="gpt-3.5-turbo"):

llm = OpenAI(model_name=model_name)
llm = OpenAI(model_name=model_name, openai_api_key=openai_api_key)
score_template = {"template": init_instructions, "score": 0.0}

df = pd.read_csv(StringIO(EXAMPLES), sep=";")
Expand Down Expand Up @@ -334,35 +334,26 @@ def run(**kwargs) -> Tuple[str, Optional[Dict[str, Any]]]:
"""Run the task"""
tool = kwargs["tool"]
prompt = kwargs["prompt"]
improve_instructions = kwargs["improve_instructions"]
max_tokens = kwargs.get("max_tokens", DEFAULT_OPENAI_SETTINGS["max_tokens"])
temperature = kwargs.get("temperature", DEFAULT_OPENAI_SETTINGS["temperature"])

openai.api_key = kwargs["api_keys"]["openai"]
openai_key = kwargs["api_keys"]["openai"]
openai.api_key = openai_key
if tool not in ALLOWED_TOOLS:
raise ValueError(f"Tool {tool} is not supported.")

engine = TOOL_TO_ENGINE[tool]
additional_information = (
fetch_additional_information(
additional_information = fetch_additional_information(
prompt=prompt,
engine=engine,
temperature=temperature,
max_tokens=max_tokens,
google_api_key=kwargs["api_keys"]["google_api_key"],
google_engine=kwargs["api_keys"]["google_engine_id"],
)
if tool == "prediction-online-sme"
else ""
)

instructions = (
prompt_engineer(PREDICTION_PROMPT_INSTRUCTIONS, PREDICTION_PROMPT_FORMAT)
if improve_instructions
else PREDICTION_PROMPT_INSTRUCTIONS
)
instructions = prompt_engineer(openai_key, PREDICTION_PROMPT_INSTRUCTIONS, PREDICTION_PROMPT_FORMAT)
instructions += PREDICTION_PROMPT_FORMAT

prediction_prompt = instructions.format(
user_prompt=prompt, additional_information=additional_information
)
Expand All @@ -386,18 +377,3 @@ def run(**kwargs) -> Tuple[str, Optional[Dict[str, Any]]]:
stop=None,
)
return response.choices[0].message.content, None


if __name__ == "__main__":
os.environ['OPENAI_API_KEY'] = "your_openai_api_key"
api_keys = {"openai": "your_openai_api_key"}

func_args = {
"api_keys": api_keys,
"tool": "deepmind-optimization",
"prompt": "Will AI take over the world in the next year?",
"improve_instructions": True,
}

response = run(**func_args)
print(response)
Loading