Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: gbnf decoding",
"type": "debugpy",
"request": "launch",
"program": "src/scenicNL/main.py",
"console": "integratedTerminal",
"args": [
"--query_path",
"Scenic-CA-AV-Crash/crash_reports/uncategorized/",
"--llm_prompt_type",
"comp_gbnf",
"--count",
"1",
"-m",
"local",
"--verbose",
"--ignore-cache",
],
"env": {
"PINECONE_API_KEY":"373c6f4b-61df-40fb-ba26-05e160088c83",
"PINECONE_ENVIRONMENT":"gcp-starter",
"PINECONE_INDEX":"scenic-programs",
}
}
]
}
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
packages=find_packages(where="src"),
package_dir={"": "src"},
install_requires=[
"anthropic",
"anthropic==0.19.1",
"bs4",
"carla==0.9.15",
"scenic==3.0.0b2",
"openai>=0.28,<=0.28.1",
"openai==1.13.3",
"pdf2image",
"pyocr",
"SQLAlchemy",
Expand Down
26 changes: 23 additions & 3 deletions src/scenicNL/adapters/anthropic_adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum
import json
from typing import cast
import os
from typing import List, cast

from anthropic import Anthropic, AI_PROMPT, HUMAN_PROMPT
import httpx
Expand All @@ -12,16 +13,22 @@
class AnthropicModel(Enum):
CLAUDE_INSTANT = "claude-instant-1.2"
CLAUDE_2 = "claude-2.0"
CLAUDE_3_STRONG = "claude-3-opus-20240229"
CLAUDE_3_MEDIUM = "claude-3-sonnet-20240229"


class AnthropicAdapter(ModelAdapter):
"""
This class servers as a wrapper for the Anthropic API.
"""
def __init__(self, model : AnthropicModel):
def __init__(self, model : AnthropicModel, use_index : bool = True):
super().__init__()
self._model = model
self.index = VectorDB(index_name='scenic-programs')
if use_index:
self.index = VectorDB(index_name='scenic-programs')
else:
self.index = None
self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def get_cache_key(
self,
Expand Down Expand Up @@ -288,6 +295,19 @@ def _format_message(
raise NotImplementedError(f"Prompt type {prompt_type} was not formatted for Anthropic model {self._model.value}")

return msg

def predict(
self,
messages: List[dict],
) -> str:
claude_response = self.client.messages.create(
messages=messages,
model=self._model.value,
max_tokens=3600 # Claude's default max tokens
)

return claude_response.content[0].text


def _predict(
self,
Expand Down
9 changes: 6 additions & 3 deletions src/scenicNL/adapters/lmql_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from scenicNL.adapters.api_adapter import Scenic3

import os
import openai
from openai import OpenAI
import json
from enum import Enum
from typing import Dict
Expand All @@ -22,9 +22,12 @@ class LMQLAdapter(ModelAdapter):
"""
def __init__(self, model: LMQLModel):
super().__init__()
openai.api_key = os.getenv("OPENAI_API_KEY")
if os.getenv("OPENAI_ORGANIZATION") and len(os.getenv("OPENAI_ORGANIZATION")) > 0:
openai.organization = os.getenv("OPENAI_ORGANIZATION")
# TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=os.getenv("OPENAI_ORGANIZATION"))'
organization = os.getenv("OPENAI_ORGANIZATION")
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"), organization=organization)
else:
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.PROMPT_PATH = os.path.join(os.curdir, 'src', 'scenicNL', 'adapters', 'prompts')
self._model = model

Expand Down
42 changes: 16 additions & 26 deletions src/scenicNL/adapters/local_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,15 @@

import requests
from scenicNL.adapters.model_adapter import ModelAdapter
from scenicNL.common import LLMPromptType, ModelInput, VectorDB
from scenicNL.common import LOCAL_MODEL_DEFAULT_PARAMS, LOCAL_MODEL_ENDPOINT, LLMPromptType, ModelInput, VectorDB
from scenicNL.constraints.gbnf_decoding import CompositionalScenic


class LocalModel(Enum):
MIXTRAL = "mixtral-8x7b"
local = "local"


class LocalAdapter(ModelAdapter):
ENDPOINT = "http://127.0.0.1:8080/completion"
DEFAULT_PARAMS = {
"cache_prompt": False,
"image_data": [],
"mirostat": 0,
"mirostat_eta": 0.1,
"mirostat_tau": 5,
"n_predict": -1,
"n_probs": 0,
"presence_penalty": 0,
"repeat_last_n": 241,
"repeat_penalty": 1.18,
"slot_id": 0,
"stop": ["Question:", "Answer:"],
#"stream": False,
"tfs_z": 1,
"top_k": 40,
"top_p": 0.5,
"typical_p": 1,
}

def __init__(self, model : LocalModel):
super().__init__()
Expand Down Expand Up @@ -68,8 +49,8 @@ def _format_message(
msg = None
# TODO: Add more prompt types

if msg is None:
raise NotImplementedError(f"Prompt type {prompt_type} was not formatted for Anthropic model {self._model.value}")
# if msg is None:
# raise NotImplementedError(f"Prompt type {prompt_type} was not formatted for Local model {self._model.value}")

return msg

Expand All @@ -83,13 +64,22 @@ def _predict(
prompt_type: LLMPromptType,
verbose: bool
) -> str:
if prompt_type == LLMPromptType.COMPOSITIONAL_GBNF:
program_generator = CompositionalScenic()
return program_generator.compositionally_construct_scenic_program(
model_input=model_input,
temperature=temperature,
max_tokens=max_length_tokens,
verbose=verbose
)

prompt = self._format_message(
model_input=model_input,
prompt_type=prompt_type,
verbose=verbose,
)

data = {"prompt": prompt, "temperature": temperature} | self.DEFAULT_PARAMS
data = {"prompt": prompt, "temperature": temperature} | LOCAL_MODEL_DEFAULT_PARAMS
if max_length_tokens > 0:
data["max_tokens"] = max_length_tokens

Expand All @@ -99,7 +89,7 @@ def _predict(
# - Add logic to check the correctness of each partial scenic program
# - Add logic to synthesize the full scenic program from all of the partial scenic programs

response = requests.post(self.ENDPOINT, json=data)
response = requests.post(LOCAL_MODEL_ENDPOINT, json=data)
if response.status_code != 200:
raise ValueError(f"Local model {self._model} returned status code {response.status_code}")
response_body = response.json()
Expand Down
2 changes: 1 addition & 1 deletion src/scenicNL/adapters/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def predict_batch(

if verbose:
print(f"Starting batch prediction using {self.__class__.__name__} " +
"with {num_workers} workers")
f"with {num_workers} workers")

with Cache(cache_path) as cache:
processor = self._batch_processor(
Expand Down
Loading