-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add two new agents into the benchmark: Autonolas with OpenAI embeddin…
…gs and Autonolas with Rephrasing questions (#5) * Evaluate question before researching it * fix var name * Add token probability for `yes` and `no` to the benchmark * fix types * Add two new agents into the benchmark: Autonolas with OpenAI embeddings and Autonolas with rephrasing questions * fix * fix * fix * fix * remove unused vars * rename * Review comments * rabbit review * remove bad merge * rabbit review * fix bad conflict
- Loading branch information
Showing
7 changed files
with
935 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import concurrent | ||
from typing import Callable, TypeVar | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
|
||
THREADPOOL = ThreadPoolExecutor(int(os.getenv("THREADPOOL_N_THREADS", 50))) | ||
|
||
A = TypeVar("A") | ||
B = TypeVar("B") | ||
|
||
def par_map( | ||
items: list[A], func: Callable[[A], B], executor: concurrent.futures.Executor = THREADPOOL | ||
) -> "list[B]": | ||
"""Applies the function to each element using the specified executor. Awaits for all results. | ||
If executor is ProcessPoolExecutor, make sure the function passed is pickable, e.g. no lambda functions | ||
""" | ||
futures: list[concurrent.futures._base.Future[B]] = [ | ||
executor.submit(func, item) for item in items | ||
] | ||
results = [] | ||
for fut in futures: | ||
results.append(fut.result()) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import json | ||
import tiktoken | ||
from pydantic import BaseModel | ||
from langchain.llms import OpenAI | ||
from langchain_openai import ChatOpenAI | ||
from evo_researcher.autonolas.research import clean_completion_json | ||
from langchain.prompts import ChatPromptTemplate | ||
|
||
|
||
QUESTION_REPHRASE_PROMPT = """We have the following question: `{question}` | ||
Write a dictionary with following keys, don't answer the question, only rewrite it in the following ways: | ||
``` | ||
- open_ended_question: Ask the question universally | ||
- negated_question Ask the question in negation | ||
``` | ||
""" | ||
|
||
|
||
class RephrasedQuestion(BaseModel): | ||
original_question: str | ||
negated_question: str | ||
open_ended_question: str | ||
|
||
|
||
def rephrase_question( | ||
question: str, | ||
engine: str = "gpt-4-0125-preview" | ||
) -> RephrasedQuestion: | ||
""" | ||
Rephrase the original question, by asking it in negation and universally, for example: | ||
original_question: Is the sky blue? | ||
negated_question: Is the sky not blue? | ||
open_ended_question: What is the color of the sky? | ||
""" | ||
tokenizer = tiktoken.encoding_for_model(engine) | ||
llm = ChatOpenAI(model=engine, temperature=0.0) | ||
|
||
prompt = ChatPromptTemplate.from_template(template=QUESTION_REPHRASE_PROMPT) | ||
messages = prompt.format_messages(question=question) | ||
|
||
max_tokens = 2 * len(tokenizer.encode(question)) + 50 # Max tokens as the question two times + some buffer for formatting. | ||
completion = llm(messages, max_tokens=max_tokens).content | ||
|
||
try: | ||
return RephrasedQuestion( | ||
original_question=question, | ||
**json.loads(clean_completion_json(completion)) | ||
) | ||
except json.decoder.JSONDecodeError as e: | ||
raise ValueError(f"Error in rephrase_question for `{question}`: {completion}") from e | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.