Skip to content

Commit

Permalink
Merge pull request #3 from codevbus/add-api-key-to-tests
Browse files Browse the repository at this point in the history
add explicit api key setting
  • Loading branch information
codevbus committed Feb 13, 2024
2 parents 275a821 + e5ca530 commit 728b875
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@
# Load anything set in .env (OpenAI API key)
load_dotenv()

os.getenv("OPENAI_API_KEY")
openai_api_key = os.getenv("OPENAI_API_KEY")


class CommaSeparatedListOutputParser(BaseOutputParser):
"""Parse the output of an LLM call to a comma-separated list."""


def parse(self, text: str):
"""Parse the output of an LLM call."""
return text.strip().split(", ")


template = """You are a helpful assistant who generates comma separated lists.
A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.
ONLY return a comma separated list, and nothing more."""
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
("system", template),
("human", human_template),
])
chain = chat_prompt | ChatOpenAI() | CommaSeparatedListOutputParser()
chat_prompt = ChatPromptTemplate.from_messages(
[
("system", template),
("human", human_template),
]
)
chain = (
chat_prompt | ChatOpenAI(api_key=openai_api_key) | CommaSeparatedListOutputParser()
)
print(chain.invoke({"text": "colors"}))

0 comments on commit 728b875

Please sign in to comment.