Skip to content

Commit a3b1c17

Browse files
authored
Merge pull request #5 from lpm0073/next
add unit tests and refactor credentials
2 parents 5fac60e + 283b8b2 commit a3b1c17

File tree

7 files changed

+107
-60
lines changed

7 files changed

+107
-60
lines changed

models/const.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
# pylint: disable=too-few-public-methods
3+
"""Sales Support Model (SSM) for the LangChain project."""
4+
5+
import os
6+
7+
from dotenv import find_dotenv, load_dotenv
8+
9+
10+
# pylint: disable=duplicate-code
11+
dotenv_path = find_dotenv()
12+
if os.path.exists(dotenv_path):
13+
load_dotenv(dotenv_path=dotenv_path, verbose=True)
14+
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
15+
OPENAI_API_ORGANIZATION = os.environ["OPENAI_API_ORGANIZATION"]
16+
PINECONE_API_KEY = os.environ["PINECONE_API_KEY"]
17+
PINECONE_ENVIRONMENT = os.environ["PINECONE_ENVIRONMENT"]
18+
PINECONE_INDEX_NAME = os.environ["PINECONE_INDEX_NAME"]
19+
else:
20+
raise FileNotFoundError("No .env file found in root directory of repository")
21+
22+
23+
class Credentials:
24+
"""Credentials."""
25+
26+
OPENAI_API_KEY = OPENAI_API_KEY
27+
OPENAI_API_ORGANIZATION = OPENAI_API_ORGANIZATION
28+
PINECONE_API_KEY = PINECONE_API_KEY
29+
PINECONE_ENVIRONMENT = PINECONE_ENVIRONMENT
30+
PINECONE_INDEX_NAME = PINECONE_INDEX_NAME

models/examples/training_services.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""Sales Support Model (SSM) for the LangChain project."""
33
import argparse
44

5-
from ..ssm import NetecPromptTemplates, SalesSupportModel
5+
from ..prompt_templates import NetecPromptTemplates
6+
from ..ssm import SalesSupportModel
67

78

89
ssm = SalesSupportModel()

models/examples/training_services_oracle.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""Sales Support Model (SSM) for the LangChain project."""
33
import argparse
44

5-
from ..ssm import NetecPromptTemplates, SalesSupportModel
5+
from ..prompt_templates import NetecPromptTemplates
6+
from ..ssm import SalesSupportModel
67

78

89
ssm = SalesSupportModel()

models/prompt_templates.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
# pylint: disable=too-few-public-methods
3+
"""Sales Support Model (SSM) prompt templates"""
4+
5+
from langchain.prompts import PromptTemplate
6+
7+
8+
class NetecPromptTemplates:
9+
"""Netec Prompt Templates."""
10+
11+
sales_role: str = """You are a helpful sales assistant at Netec who sells
12+
specialized training and exam preparation services to existing customers.
13+
You provide concise explanations of the services that Netec offers in 100
14+
words or less."""
15+
16+
@classmethod
17+
def get_properties(cls):
18+
"""return a list of properties of this class."""
19+
return [attr for attr in dir(cls) if isinstance(getattr(cls, attr), property)]
20+
21+
@property
22+
def training_services(self) -> PromptTemplate:
23+
"""Get prompt."""
24+
template = (
25+
self.sales_role
26+
+ """
27+
Explain the training services that Netec offers about {concept}
28+
"""
29+
)
30+
return PromptTemplate(input_variables=["concept"], template=template)
31+
32+
@property
33+
def oracle_training_services(self) -> PromptTemplate:
34+
"""Get prompt."""
35+
template = (
36+
self.sales_role
37+
+ """
38+
Note that Netec is the exclusive provide of Oracle training services
39+
for the 6 levels of Oracle Certification credentials: Oracle Certified Junior Associate (OCJA),
40+
Oracle Certified Associate (OCA), Oracle Certified Professional (OCP),
41+
Oracle Certified Master (OCM), Oracle Certified Expert (OCE) and
42+
Oracle Certified Specialist (OCS).
43+
Summarize their programs for {concept}
44+
"""
45+
)
46+
return PromptTemplate(input_variables=["concept"], template=template)

models/ssm.py

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
# pylint: disable=too-few-public-methods
33
"""Sales Support Model (SSM) for the LangChain project."""
44

5-
import os
65
from typing import ClassVar, List
76

87
import pinecone
9-
from dotenv import find_dotenv, load_dotenv
108
from langchain.chat_models import ChatOpenAI
119
from langchain.embeddings import OpenAIEmbeddings
1210
from langchain.llms.openai import OpenAI
@@ -16,62 +14,11 @@
1614
from langchain.vectorstores.pinecone import Pinecone
1715
from pydantic import BaseModel, ConfigDict, Field # ValidationError
1816

17+
from models.const import Credentials
1918

20-
# pylint: disable=duplicate-code
21-
dotenv_path = find_dotenv()
22-
if os.path.exists(dotenv_path):
23-
load_dotenv(dotenv_path=dotenv_path, verbose=True)
24-
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
25-
OPENAI_API_ORGANIZATION = os.environ["OPENAI_API_ORGANIZATION"]
26-
PINECONE_API_KEY = os.environ["PINECONE_API_KEY"]
27-
PINECONE_ENVIRONMENT = os.environ["PINECONE_ENVIRONMENT"]
28-
PINECONE_INDEX_NAME = os.environ["PINECONE_INDEX_NAME"]
29-
else:
30-
raise FileNotFoundError("No .env file found in root directory of repository")
3119

3220
DEFAULT_MODEL_NAME = "text-davinci-003"
33-
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
34-
35-
36-
class NetecPromptTemplates:
37-
"""Netec Prompt Templates."""
38-
39-
sales_role: str = """You are a helpful sales assistant at Netec who sells
40-
specialized training and exam preparation services to existing customers.
41-
You provide concise explanations of the services that Netec offers in 100
42-
words or less."""
43-
44-
@classmethod
45-
def get_properties(cls):
46-
"""return a list of properties of this class."""
47-
return [attr for attr in dir(cls) if isinstance(getattr(cls, attr), property)]
48-
49-
@property
50-
def training_services(self) -> PromptTemplate:
51-
"""Get prompt."""
52-
template = (
53-
self.sales_role
54-
+ """
55-
Explain the training services that Netec offers about {concept}
56-
"""
57-
)
58-
return PromptTemplate(input_variables=["concept"], template=template)
59-
60-
@property
61-
def oracle_training_services(self) -> PromptTemplate:
62-
"""Get prompt."""
63-
template = (
64-
self.sales_role
65-
+ """
66-
Note that Netec is the exclusive provide of Oracle training services
67-
for the 6 levels of Oracle Certification credentials: Oracle Certified Junior Associate (OCJA),
68-
Oracle Certified Associate (OCA), Oracle Certified Professional (OCP),
69-
Oracle Certified Master (OCM), Oracle Certified Expert (OCE) and
70-
Oracle Certified Specialist (OCS).
71-
Summarize their programs for {concept}
72-
"""
73-
)
74-
return PromptTemplate(input_variables=["concept"], template=template)
21+
pinecone.init(api_key=Credentials.PINECONE_API_KEY, environment=Credentials.PINECONE_ENVIRONMENT)
7522

7623

7724
class SalesSupportModel(BaseModel):
@@ -82,8 +29,8 @@ class SalesSupportModel(BaseModel):
8229
# prompting wrapper
8330
chat: ChatOpenAI = Field(
8431
default_factory=lambda: ChatOpenAI(
85-
api_key=OPENAI_API_KEY,
86-
organization=OPENAI_API_ORGANIZATION,
32+
api_key=Credentials.OPENAI_API_KEY,
33+
organization=Credentials.OPENAI_API_ORGANIZATION,
8734
max_retries=3,
8835
model="gpt-3.5-turbo",
8936
temperature=0.3,

models/tests/test_base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@
66
"""
77
import pytest # pylint: disable=unused-import
88

9+
from ..prompt_templates import NetecPromptTemplates
910
from ..ssm import SalesSupportModel
1011

1112

1213
class TestSalesSupportModel:
1314
"""Test SalesSupportModel class."""
1415

16+
ssm = SalesSupportModel()
17+
templates = NetecPromptTemplates()
18+
1519
def test_01_basic(self):
1620
"""Test a basic request"""
1721

1822
SalesSupportModel()
23+
24+
def test_oracle_training_services(self):
25+
"""Test a prompt with the Oracle training services template"""
26+
27+
prompt = self.templates.oracle_training_services
28+
result = self.ssm.prompt_with_template(prompt=prompt, concept="Oracle database administrator")
29+
assert result
30+
assert "Oracle" in result
31+
assert "training" in result
32+
33+
def test_training_services(self):
34+
"""Test a prompt with the training services template"""
35+
36+
prompt = self.templates.training_services
37+
result = self.ssm.prompt_with_template(prompt=prompt, concept="Microsoft certified Azure AI engineer associate")
38+
assert result
39+
assert "Microsoft" in result
40+
assert "training" in result

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ extend-exclude = "*__init__.py,*__version__.py,venv"
3434
select = "C101"
3535

3636
[tool.codespell]
37-
skip = '*.svg,models/ssm.py'
37+
skip = '*.svg,models/prompt_templates.py'
3838
ignore-words = 'codespell.txt'

0 commit comments

Comments
 (0)