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

Enable OpenAI other compatible APIs #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions browserpilot/agents/compilers/instruction_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import traceback
import os
import re

from typing import Dict, List, Union

Expand All @@ -16,8 +17,8 @@
logger = logging.getLogger(__name__)

# Instantiate OpenAI with OPENAI_API_KEY.
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY").strip(),
base_url=os.environ.get("OPENAI_API_BASE_URL", None))
"""Set up all the prompt variables."""

# Designated tokens.
Expand Down Expand Up @@ -300,6 +301,21 @@ def get_completion(
stop=stop,
)
text = response.choices[0].message.content
elif'api.openai.com' not in str(client.base_url):
# LLama3 is more aggressive and stops at the first stop token. might want
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=max_tokens,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
temperature=temperature
)
raw_text = response.choices[0].message.content
text = "\n".join(re.findall(r"```([^`]+)```", raw_text))


else:
response = client.completions.create(
model=model,
Expand Down Expand Up @@ -335,6 +351,7 @@ def get_action_output(self, instructions):
"""Get the action output for the given instructions."""
prompt = self.base_prompt.format(instructions=instructions)
completion = self.get_completion(prompt).strip()

action_output = completion.strip()
lines = [line for line in action_output.split("\n") if not line.startswith("import ")]
action_output = "\n".join(lines)
Expand Down