A fix for Jupyter environment variable issue (#135)

- fixes the Jupyter environment variable issues mentioned in issue #134 
- fixes format/lint issues in some unrelated files (from make
format/lint)


![image](https://user-images.githubusercontent.com/347398/201599322-090af858-362d-4d69-bf59-208aea65419a.png)
This commit is contained in:
Delip Rao 2022-11-14 11:34:01 -05:00 committed by GitHub
parent ced29b816b
commit 76cecf8165
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 51 additions and 59 deletions

View File

@ -59,9 +59,7 @@ class MapReduceChain(Chain, BaseModel):
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]: def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
# Split the larger text into smaller chunks. # Split the larger text into smaller chunks.
docs = self.text_splitter.split_text( docs = self.text_splitter.split_text(inputs[self.input_key],)
inputs[self.input_key],
)
# Now that we have the chunks, we send them to the LLM and track results. # Now that we have the chunks, we send them to the LLM and track results.
# This is the "map" part. # This is the "map" part.
summaries = [] summaries = []

View File

@ -28,13 +28,7 @@ class Crawler:
"Could not import playwright python package. " "Could not import playwright python package. "
"Please it install it with `pip install playwright`." "Please it install it with `pip install playwright`."
) )
self.browser = ( self.browser = sync_playwright().start().chromium.launch(headless=False,)
sync_playwright()
.start()
.chromium.launch(
headless=False,
)
)
self.page = self.browser.new_page() self.page = self.browser.new_page()
self.page.set_viewport_size({"width": 1280, "height": 1080}) self.page.set_viewport_size({"width": 1280, "height": 1080})

View File

@ -109,8 +109,4 @@ Action 3: Finish[yes]""",
] ]
SUFFIX = """\n\nQuestion: {input}""" SUFFIX = """\n\nQuestion: {input}"""
PROMPT = Prompt.from_examples( PROMPT = Prompt.from_examples(EXAMPLES, SUFFIX, ["input"],)
EXAMPLES,
SUFFIX,
["input"],
)

View File

@ -38,7 +38,4 @@ Intermediate Answer: New Zealand.
So the final answer is: No So the final answer is: No
Question: {input}""" Question: {input}"""
PROMPT = Prompt( PROMPT = Prompt(input_variables=["input"], template=_DEFAULT_TEMPLATE,)
input_variables=["input"],
template=_DEFAULT_TEMPLATE,
)

View File

@ -15,6 +15,5 @@ Only use the following tables:
Question: {input}""" Question: {input}"""
PROMPT = Prompt( PROMPT = Prompt(
input_variables=["input", "table_info", "dialect"], input_variables=["input", "table_info", "dialect"], template=_DEFAULT_TEMPLATE,
template=_DEFAULT_TEMPLATE,
) )

View File

@ -1,10 +1,10 @@
"""Wrapper around Cohere embedding models.""" """Wrapper around Cohere embedding models."""
import os
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.embeddings.base import Embeddings from langchain.embeddings.base import Embeddings
from langchain.llms.utils import get_from_dict_or_env
class CohereEmbeddings(BaseModel, Embeddings): class CohereEmbeddings(BaseModel, Embeddings):
@ -25,7 +25,7 @@ class CohereEmbeddings(BaseModel, Embeddings):
model: str = "medium" model: str = "medium"
"""Model name to use.""" """Model name to use."""
cohere_api_key: Optional[str] = os.environ.get("COHERE_API_KEY") cohere_api_key: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -35,7 +35,9 @@ class CohereEmbeddings(BaseModel, Embeddings):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
cohere_api_key = values.get("cohere_api_key") cohere_api_key = get_from_dict_or_env(
values, "cohere_api_key", "COHERE_API_KEY"
)
if cohere_api_key is None or cohere_api_key == "": if cohere_api_key is None or cohere_api_key == "":
raise ValueError( raise ValueError(

View File

@ -1,10 +1,10 @@
"""Wrapper around OpenAI embedding models.""" """Wrapper around OpenAI embedding models."""
import os
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.embeddings.base import Embeddings from langchain.embeddings.base import Embeddings
from langchain.llms.utils import get_from_dict_or_env
class OpenAIEmbeddings(BaseModel, Embeddings): class OpenAIEmbeddings(BaseModel, Embeddings):
@ -25,7 +25,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
model_name: str = "babbage" model_name: str = "babbage"
"""Model name to use.""" """Model name to use."""
openai_api_key: Optional[str] = os.environ.get("OPENAI_API_KEY") openai_api_key: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -35,7 +35,9 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
openai_api_key = values.get("openai_api_key") openai_api_key = get_from_dict_or_env(
values, "openai_api_key", "OPENAI_API_KEY"
)
if openai_api_key is None or openai_api_key == "": if openai_api_key is None or openai_api_key == "":
raise ValueError( raise ValueError(

View File

@ -1,11 +1,11 @@
"""Wrapper around AI21 APIs.""" """Wrapper around AI21 APIs."""
import os
from typing import Any, Dict, List, Mapping, Optional from typing import Any, Dict, List, Mapping, Optional
import requests import requests
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.llms.utils import get_from_dict_or_env
class AI21PenaltyData(BaseModel): class AI21PenaltyData(BaseModel):
@ -62,7 +62,7 @@ class AI21(BaseModel, LLM):
logitBias: Optional[Dict[str, float]] = None logitBias: Optional[Dict[str, float]] = None
"""Adjust the probability of specific tokens being generated.""" """Adjust the probability of specific tokens being generated."""
ai21_api_key: Optional[str] = os.environ.get("AI21_API_KEY") ai21_api_key: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -72,8 +72,7 @@ class AI21(BaseModel, LLM):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key exists in environment.""" """Validate that api key exists in environment."""
ai21_api_key = values.get("ai21_api_key") ai21_api_key = get_from_dict_or_env(values, "ai21_api_key", "AI21_API_KEY")
if ai21_api_key is None or ai21_api_key == "": if ai21_api_key is None or ai21_api_key == "":
raise ValueError( raise ValueError(
"Did not find AI21 API key, please add an environment variable" "Did not find AI21 API key, please add an environment variable"
@ -122,11 +121,7 @@ class AI21(BaseModel, LLM):
response = requests.post( response = requests.post(
url=f"https://api.ai21.com/studio/v1/{self.model}/complete", url=f"https://api.ai21.com/studio/v1/{self.model}/complete",
headers={"Authorization": f"Bearer {self.ai21_api_key}"}, headers={"Authorization": f"Bearer {self.ai21_api_key}"},
json={ json={"prompt": prompt, "stopSequences": stop, **self._default_params,},
"prompt": prompt,
"stopSequences": stop,
**self._default_params,
},
) )
if response.status_code != 200: if response.status_code != 200:
optional_detail = response.json().get("error") optional_detail = response.json().get("error")

View File

@ -1,11 +1,10 @@
"""Wrapper around Cohere APIs.""" """Wrapper around Cohere APIs."""
import os
from typing import Any, Dict, List, Mapping, Optional from typing import Any, Dict, List, Mapping, Optional
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens from langchain.llms.utils import enforce_stop_tokens, get_from_dict_or_env
class Cohere(LLM, BaseModel): class Cohere(LLM, BaseModel):
@ -44,7 +43,7 @@ class Cohere(LLM, BaseModel):
presence_penalty: int = 0 presence_penalty: int = 0
"""Penalizes repeated tokens.""" """Penalizes repeated tokens."""
cohere_api_key: Optional[str] = os.environ.get("COHERE_API_KEY") cohere_api_key: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -54,7 +53,9 @@ class Cohere(LLM, BaseModel):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
cohere_api_key = values.get("cohere_api_key") cohere_api_key = get_from_dict_or_env(
values, "cohere_api_key", "COHERE_API_KEY"
)
if cohere_api_key is None or cohere_api_key == "": if cohere_api_key is None or cohere_api_key == "":
raise ValueError( raise ValueError(

View File

@ -1,11 +1,10 @@
"""Wrapper around HuggingFace APIs.""" """Wrapper around HuggingFace APIs."""
import os
from typing import Any, Dict, List, Mapping, Optional from typing import Any, Dict, List, Mapping, Optional
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens from langchain.llms.utils import enforce_stop_tokens, get_from_dict_or_env
DEFAULT_REPO_ID = "gpt2" DEFAULT_REPO_ID = "gpt2"
VALID_TASKS = ("text2text-generation", "text-generation") VALID_TASKS = ("text2text-generation", "text-generation")
@ -18,7 +17,7 @@ class HuggingFaceHub(LLM, BaseModel):
environment variable ``HUGGINGFACEHUB_API_TOKEN`` set with your API token, or pass environment variable ``HUGGINGFACEHUB_API_TOKEN`` set with your API token, or pass
it as a named parameter to the constructor. it as a named parameter to the constructor.
Only supports task `text-generation` for now. Only supports `text-generation` and `text2text-generation` for now.
Example: Example:
.. code-block:: python .. code-block:: python
@ -35,7 +34,7 @@ class HuggingFaceHub(LLM, BaseModel):
model_kwargs: Optional[dict] = None model_kwargs: Optional[dict] = None
"""Key word arguments to pass to the model.""" """Key word arguments to pass to the model."""
huggingfacehub_api_token: Optional[str] = os.environ.get("HUGGINGFACEHUB_API_TOKEN") huggingfacehub_api_token: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -45,7 +44,9 @@ class HuggingFaceHub(LLM, BaseModel):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
huggingfacehub_api_token = values.get("huggingfacehub_api_token") huggingfacehub_api_token = get_from_dict_or_env(
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN"
)
if huggingfacehub_api_token is None or huggingfacehub_api_token == "": if huggingfacehub_api_token is None or huggingfacehub_api_token == "":
raise ValueError( raise ValueError(
"Did not find HuggingFace API token, please add an environment variable" "Did not find HuggingFace API token, please add an environment variable"

View File

@ -1,10 +1,10 @@
"""Wrapper around NLPCloud APIs.""" """Wrapper around NLPCloud APIs."""
import os
from typing import Any, Dict, List, Mapping, Optional from typing import Any, Dict, List, Mapping, Optional
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.llms.utils import get_from_dict_or_env
class NLPCloud(LLM, BaseModel): class NLPCloud(LLM, BaseModel):
@ -54,7 +54,7 @@ class NLPCloud(LLM, BaseModel):
num_return_sequences: int = 1 num_return_sequences: int = 1
"""How many completions to generate for each prompt.""" """How many completions to generate for each prompt."""
nlpcloud_api_key: Optional[str] = os.environ.get("NLPCLOUD_API_KEY") nlpcloud_api_key: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -64,7 +64,9 @@ class NLPCloud(LLM, BaseModel):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
nlpcloud_api_key = values.get("nlpcloud_api_key") nlpcloud_api_key = get_from_dict_or_env(
values, "nlpcloud_api_key", "NLPCLOUD_API_KEY"
)
if nlpcloud_api_key is None or nlpcloud_api_key == "": if nlpcloud_api_key is None or nlpcloud_api_key == "":
raise ValueError( raise ValueError(

View File

@ -1,10 +1,10 @@
"""Wrapper around OpenAI APIs.""" """Wrapper around OpenAI APIs."""
import os
from typing import Any, Dict, List, Mapping, Optional from typing import Any, Dict, List, Mapping, Optional
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, Extra, root_validator
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.llms.utils import get_from_dict_or_env
class OpenAI(LLM, BaseModel): class OpenAI(LLM, BaseModel):
@ -38,7 +38,7 @@ class OpenAI(LLM, BaseModel):
best_of: int = 1 best_of: int = 1
"""Generates best_of completions server-side and returns the "best".""" """Generates best_of completions server-side and returns the "best"."""
openai_api_key: Optional[str] = os.environ.get("OPENAI_API_KEY") openai_api_key: Optional[str] = None
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
@ -48,7 +48,9 @@ class OpenAI(LLM, BaseModel):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
openai_api_key = values.get("openai_api_key") openai_api_key = get_from_dict_or_env(
values, "openai_api_key", "OPENAI_API_KEY"
)
if openai_api_key is None or openai_api_key == "": if openai_api_key is None or openai_api_key == "":
raise ValueError( raise ValueError(

View File

@ -1,8 +1,16 @@
"""Common utility functions for working with LLM APIs.""" """Common utility functions for working with LLM APIs."""
import os
import re import re
from typing import List from typing import Any, Dict, List
def enforce_stop_tokens(text: str, stop: List[str]) -> str: def enforce_stop_tokens(text: str, stop: List[str]) -> str:
"""Cut off the text as soon as any stop words occur.""" """Cut off the text as soon as any stop words occur."""
return re.split("|".join(stop), text)[0] return re.split("|".join(stop), text)[0]
def get_from_dict_or_env(data: Dict[str, Any], key: str, env_key: str) -> Any:
"""Get a value from a dictionary or an environment variable."""
if key in data:
return data[key]
return os.environ.get(env_key, None)

View File

@ -45,10 +45,7 @@ class ElasticVectorSearch(VectorStore):
""" """
def __init__( def __init__(
self, self, elasticsearch_url: str, index_name: str, embedding_function: Callable,
elasticsearch_url: str,
index_name: str,
embedding_function: Callable,
): ):
"""Initialize with necessary components.""" """Initialize with necessary components."""
try: try:

View File

@ -6,9 +6,7 @@ def test_manifest_wrapper() -> None:
"""Test manifest wrapper.""" """Test manifest wrapper."""
from manifest import Manifest from manifest import Manifest
manifest = Manifest( manifest = Manifest(client_name="openai",)
client_name="openai",
)
llm = ManifestWrapper(client=manifest, llm_kwargs={"temperature": 0}) llm = ManifestWrapper(client=manifest, llm_kwargs={"temperature": 0})
output = llm("The capital of New York is:") output = llm("The capital of New York is:")
assert output == "Albany" assert output == "Albany"