Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 905c1ca

Browse files
committed
fun facts! Establish the API call to open AI to get a fun fact about programming and AI.
1 parent 99acf05 commit 905c1ca

File tree

5 files changed

+120
-9
lines changed

5 files changed

+120
-9
lines changed

cli.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
from dotenv import find_dotenv, load_dotenv
2+
from langchain.chains import LLMChain
3+
from langchain.chat_models import ChatOpenAI
4+
from langchain.prompts import load_prompt
25
from pathlib import Path
36
from rich.console import Console
7+
from rich.style import Style
48
from setup import __version__
5-
import click, os, sys, webbrowser
9+
import click, datetime, openai, os, random, sys, webbrowser
610

711
# Create a Console object
812
console = Console()
13+
bot_style = Style(color="#30D5C8")
914

1015
# Load environment variables from .env file
1116
load_dotenv(find_dotenv())
1217

1318

1419
def setup_environment():
1520
if os.getenv("OPENAI_API_KEY"):
21+
openai.api_key = os.getenv("OPENAI_API_KEY")
1622
return True
1723

1824
openai_api_key_url = "https://platform.openai.com/account/api-keys"
@@ -63,12 +69,21 @@ def version():
6369

6470
@cli.command()
6571
@click.option("-v", "--verbose", count=True)
66-
def joke(verbose):
67-
"""Tell a [probably bad] programming joke."""
72+
def funfact(verbose):
73+
"""Tell me something interesting about programming or AI"""
6874
setup_environment()
69-
api_key = os.getenv("OPENAI_API_KEY")
70-
if verbose:
71-
console.print(f"[bold yellow]Using API key: {api_key}[/bold yellow]")
75+
76+
prompt = load_prompt(Path(__file__).parent / "prompts" / "fun_fact.yaml")
77+
78+
llm = ChatOpenAI(temperature=1, max_tokens=1024)
79+
# Set up the chain
80+
chat_chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose)
81+
82+
with console.status("Thinking", spinner="point"):
83+
# Select a random year from 1950 to current year so that we get a different answer each time
84+
year = random.randint(1950, datetime.datetime.utcnow().year)
85+
response = chat_chain.run(f"programming and artificial intelligence in the year {year}")
86+
console.print(response, style=bot_style)
7287

7388

7489
if __name__ == "__main__":

prompts/fun_fact.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
_type: prompt
2+
template_format: f-string
3+
input_variables: ["topic"]
4+
template: |
5+
You are history nerd who loves sharing information.
6+
Your expertise is {topic}.
7+
You love emojis.
8+
9+
Tell me a fun fact.

requirements/requirements.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
click
2+
langchain
3+
openai
24
python-dotenv
35
rich

requirements/requirements.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,100 @@
44
#
55
# pip-compile
66
#
7+
aiohttp==3.8.4
8+
# via
9+
# langchain
10+
# openai
11+
aiosignal==1.3.1
12+
# via aiohttp
13+
async-timeout==4.0.2
14+
# via aiohttp
15+
attrs==23.1.0
16+
# via aiohttp
17+
certifi==2023.5.7
18+
# via requests
19+
charset-normalizer==3.1.0
20+
# via
21+
# aiohttp
22+
# requests
723
click==8.1.3
824
# via -r requirements.in
25+
dataclasses-json==0.5.8
26+
# via langchain
27+
frozenlist==1.3.3
28+
# via
29+
# aiohttp
30+
# aiosignal
31+
idna==3.4
32+
# via
33+
# requests
34+
# yarl
35+
langchain==0.0.207
36+
# via -r requirements.in
37+
langchainplus-sdk==0.0.16
38+
# via langchain
939
markdown-it-py==3.0.0
1040
# via rich
41+
marshmallow==3.19.0
42+
# via
43+
# dataclasses-json
44+
# marshmallow-enum
45+
marshmallow-enum==1.5.1
46+
# via dataclasses-json
1147
mdurl==0.1.2
1248
# via markdown-it-py
49+
multidict==6.0.4
50+
# via
51+
# aiohttp
52+
# yarl
53+
mypy-extensions==1.0.0
54+
# via typing-inspect
55+
numexpr==2.8.4
56+
# via langchain
57+
numpy==1.25.0
58+
# via
59+
# langchain
60+
# numexpr
61+
openai==0.27.8
62+
# via -r requirements.in
63+
openapi-schema-pydantic==1.2.4
64+
# via langchain
65+
packaging==23.1
66+
# via marshmallow
67+
pydantic==1.10.9
68+
# via
69+
# langchain
70+
# langchainplus-sdk
71+
# openapi-schema-pydantic
1372
pygments==2.15.1
1473
# via rich
1574
python-dotenv==1.0.0
1675
# via -r requirements.in
76+
pyyaml==6.0
77+
# via langchain
78+
requests==2.31.0
79+
# via
80+
# langchain
81+
# langchainplus-sdk
82+
# openai
1783
rich==13.4.2
1884
# via -r requirements.in
85+
sqlalchemy==2.0.16
86+
# via langchain
87+
tenacity==8.2.2
88+
# via
89+
# langchain
90+
# langchainplus-sdk
91+
tqdm==4.65.0
92+
# via openai
93+
typing-extensions==4.6.3
94+
# via
95+
# pydantic
96+
# sqlalchemy
97+
# typing-inspect
98+
typing-inspect==0.9.0
99+
# via dataclasses-json
100+
urllib3==2.0.3
101+
# via requests
102+
yarl==1.9.2
103+
# via aiohttp

test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from cli import joke, version
1+
from cli import funfact, version
22
from click.testing import CliRunner
33
from setup import __version__
44
import os, pytest
@@ -12,7 +12,7 @@ def test_version():
1212

1313

1414
@pytest.mark.skipif(os.getenv("OPENAI_API_KEY") is None, reason="Skipping live tests without an API key.")
15-
def test_joke():
15+
def test_funfact():
1616
runner = CliRunner()
17-
result = runner.invoke(joke)
17+
result = runner.invoke(funfact)
1818
assert result.exit_code == 0

0 commit comments

Comments
 (0)