AutoGPT/autogpt/llm_utils.py

186 lines
6.4 KiB
Python
Raw Normal View History

2023-04-16 18:02:48 +00:00
from __future__ import annotations
import time
2023-04-17 19:42:17 +00:00
from typing import List, Optional
2023-04-14 19:42:28 +00:00
import openai
from colorama import Fore, Style
2023-04-17 02:51:36 +00:00
from openai.error import APIError, RateLimitError
2023-04-14 19:42:28 +00:00
from autogpt.config import Config
from autogpt.logs import logger
2023-04-18 23:59:23 +00:00
from autogpt.types.openai import Message
CFG = Config()
openai.api_key = CFG.openai_api_key
def call_ai_function(
2023-04-16 18:02:48 +00:00
function: str, args: list, description: str, model: str | None = None
) -> str:
"""Call an AI function
This is a magic function that can do anything with no-code. See
https://github.com/Torantulino/AI-Functions for more info.
Args:
function (str): The function to call
args (list): The arguments to pass to the function
description (str): The description of the function
model (str, optional): The model to use. Defaults to None.
Returns:
str: The response from the function
"""
if model is None:
model = CFG.smart_llm_model
# For each arg, if any are None, convert to "None":
args = [str(arg) if arg is not None else "None" for arg in args]
# parse args to comma separated string
2023-04-17 19:42:17 +00:00
args: str = ", ".join(args)
messages: List[Message] = [
{
"role": "system",
"content": f"You are now the following python function: ```# {description}"
f"\n{function}```\n\nOnly respond with your `return` value.",
},
{"role": "user", "content": args},
]
return create_chat_completion(model=model, messages=messages, temperature=0)
# Overly simple abstraction until we create something better
# simple retry mechanism when getting a rate error or a bad gateway
2023-04-14 19:42:28 +00:00
def create_chat_completion(
2023-04-17 19:42:17 +00:00
messages: List[Message], # type: ignore
model: Optional[str] = None,
temperature: float = CFG.temperature,
2023-04-17 19:42:17 +00:00
max_tokens: Optional[int] = None,
2023-04-14 19:42:28 +00:00
) -> str:
"""Create a chat completion using the OpenAI API
Args:
2023-04-17 19:42:17 +00:00
messages (List[Message]): The messages to send to the chat completion
model (str, optional): The model to use. Defaults to None.
temperature (float, optional): The temperature to use. Defaults to 0.9.
max_tokens (int, optional): The max tokens to use. Defaults to None.
Returns:
str: The response from the chat completion
"""
num_retries = 10
warned_user = False
if CFG.debug_mode:
print(
2023-04-17 19:44:54 +00:00
f"{Fore.GREEN}Creating chat completion with model {model}, temperature {temperature}, max_tokens {max_tokens}{Fore.RESET}"
)
2023-04-17 04:30:42 +00:00
for plugin in CFG.plugins:
if plugin.can_handle_chat_completion(
messages=messages,
model=model,
temperature=temperature,
max_tokens=max_tokens,
):
2023-04-19 23:17:04 +00:00
message = plugin.handle_chat_completion(
2023-04-17 04:30:42 +00:00
messages=messages,
model=model,
temperature=temperature,
max_tokens=max_tokens,
)
2023-04-19 23:17:04 +00:00
if message is not None:
return message
2023-04-17 19:44:54 +00:00
response = None
for attempt in range(num_retries):
backoff = 2 ** (attempt + 2)
try:
if CFG.use_azure:
response = openai.ChatCompletion.create(
deployment_id=CFG.get_azure_deployment_id_for_model(model),
model=model,
messages=messages,
temperature=temperature,
2023-04-14 19:42:28 +00:00
max_tokens=max_tokens,
)
else:
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
2023-04-14 19:42:28 +00:00
max_tokens=max_tokens,
)
break
except RateLimitError:
if CFG.debug_mode:
print(
f"{Fore.RED}Error: ", f"Reached rate limit, passing...{Fore.RESET}"
)
if not warned_user:
logger.double_check(
2023-04-17 20:41:42 +00:00
f"Please double check that you have setup a {Fore.CYAN + Style.BRIGHT}PAID{Style.RESET_ALL} OpenAI API Account. "
+ f"You can read more here: {Fore.CYAN}https://github.com/Significant-Gravitas/Auto-GPT#openai-api-keys-configuration{Fore.RESET}"
)
warned_user = True
except APIError as e:
2023-04-17 19:44:54 +00:00
if e.http_status != 502:
raise
if attempt == num_retries - 1:
raise
if CFG.debug_mode:
print(
2023-04-17 19:44:54 +00:00
f"{Fore.RED}Error: ",
f"API Bad gateway. Waiting {backoff} seconds...{Fore.RESET}",
)
time.sleep(backoff)
if response is None:
logger.typewriter_log(
2023-04-16 19:16:43 +00:00
"FAILED TO GET RESPONSE FROM OPENAI",
Fore.RED,
2023-04-17 20:41:42 +00:00
"Auto-GPT has failed to get a response from OpenAI's services. "
+ f"Try running Auto-GPT again, and if the problem the persists try running it with `{Fore.CYAN}--debug{Fore.RESET}`.",
)
logger.double_check()
if CFG.debug_mode:
raise RuntimeError(f"Failed to get response after {num_retries} retries")
else:
quit(1)
resp = response.choices[0].message["content"]
for plugin in CFG.plugins:
2023-04-17 04:39:33 +00:00
if not plugin.can_handle_on_response():
continue
resp = plugin.on_response(resp)
return resp
def create_embedding_with_ada(text) -> list:
2023-04-17 12:25:49 +00:00
"""Create an embedding with text-ada-002 using the OpenAI SDK"""
num_retries = 10
for attempt in range(num_retries):
backoff = 2 ** (attempt + 2)
try:
if CFG.use_azure:
2023-04-15 21:40:12 +00:00
return openai.Embedding.create(
input=[text],
engine=CFG.get_azure_deployment_id_for_model(
"text-embedding-ada-002"
),
)["data"][0]["embedding"]
else:
2023-04-15 21:40:12 +00:00
return openai.Embedding.create(
input=[text], model="text-embedding-ada-002"
)["data"][0]["embedding"]
except RateLimitError:
pass
except APIError as e:
2023-04-17 19:44:54 +00:00
if e.http_status != 502:
raise
if attempt == num_retries - 1:
raise
if CFG.debug_mode:
print(
2023-04-17 19:44:54 +00:00
f"{Fore.RED}Error: ",
f"API Bad gateway. Waiting {backoff} seconds...{Fore.RESET}",
)
time.sleep(backoff)