David Chu commited on
Commit
00d1644
·
unverified ·
1 Parent(s): 3440d6a

refactor: use pydantic settings

Browse files
Files changed (6) hide show
  1. app/config.py +10 -0
  2. app/main.py +2 -4
  3. app/tools/literature.py +0 -1
  4. main.py +3 -3
  5. pyproject.toml +1 -0
  6. uv.lock +16 -0
app/config.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic_settings import BaseSettings, SettingsConfigDict
2
+
3
+
4
+ class Settings(BaseSettings):
5
+ google_api_key: str
6
+
7
+ model_config = SettingsConfigDict(env_file=".env")
8
+
9
+
10
+ settings = Settings() # type: ignore
app/main.py CHANGED
@@ -1,14 +1,12 @@
1
- import os
2
-
3
  from fastapi import FastAPI
4
  from google import genai
5
  from pydantic import BaseModel
6
 
7
- from app import agent
8
 
 
9
  app = FastAPI()
10
 
11
- gemini = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
12
 
13
 
14
  class Source(BaseModel):
 
 
 
1
  from fastapi import FastAPI
2
  from google import genai
3
  from pydantic import BaseModel
4
 
5
+ from app import agent, config
6
 
7
+ gemini = genai.Client(api_key=config.settings.google_api_key)
8
  app = FastAPI()
9
 
 
10
 
11
 
12
  class Source(BaseModel):
app/tools/literature.py CHANGED
@@ -1,4 +1,3 @@
1
- import time
2
  from xml.etree import ElementTree
3
 
4
  import httpx
 
 
1
  from xml.etree import ElementTree
2
 
3
  import httpx
main.py CHANGED
@@ -7,7 +7,7 @@ import streamlit as st
7
  from google import genai
8
  from google.genai import types
9
 
10
- from app.agent import respond
11
 
12
 
13
  def format_output(response: list[dict]) -> tuple[str, str]:
@@ -39,7 +39,7 @@ def format_output(response: list[dict]) -> tuple[str, str]:
39
 
40
 
41
  def main():
42
- gemini_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
43
 
44
  st.title("Elna")
45
  with st.form("search", border=False):
@@ -49,7 +49,7 @@ def main():
49
 
50
  if submit:
51
  with st.spinner("Thinking...", show_time=True):
52
- output = respond(gemini_client, query)
53
 
54
  answer, footnotes = format_output(output)
55
  response.markdown(f"{answer}\n\n{footnotes}")
 
7
  from google import genai
8
  from google.genai import types
9
 
10
+ from app import agent, config
11
 
12
 
13
  def format_output(response: list[dict]) -> tuple[str, str]:
 
39
 
40
 
41
  def main():
42
+ gemini_client = genai.Client(api_key=config.settings.google_api_key)
43
 
44
  st.title("Elna")
45
  with st.form("search", border=False):
 
49
 
50
  if submit:
51
  with st.spinner("Thinking...", show_time=True):
52
+ output = agent.respond(gemini_client, query)
53
 
54
  answer, footnotes = format_output(output)
55
  response.markdown(f"{answer}\n\n{footnotes}")
pyproject.toml CHANGED
@@ -9,6 +9,7 @@ dependencies = [
9
  "google-genai>=1.14.0",
10
  "httpx>=0.28.1",
11
  "pydantic>=2.11.4",
 
12
  "python-fasthtml>=0.12.15",
13
  "streamlit>=1.45.0",
14
  "tenacity>=9.1.2",
 
9
  "google-genai>=1.14.0",
10
  "httpx>=0.28.1",
11
  "pydantic>=2.11.4",
12
+ "pydantic-settings>=2.9.1",
13
  "python-fasthtml>=0.12.15",
14
  "streamlit>=1.45.0",
15
  "tenacity>=9.1.2",
uv.lock CHANGED
@@ -205,6 +205,7 @@ dependencies = [
205
  { name = "google-genai" },
206
  { name = "httpx" },
207
  { name = "pydantic" },
 
208
  { name = "python-fasthtml" },
209
  { name = "streamlit" },
210
  { name = "tenacity" },
@@ -216,6 +217,7 @@ requires-dist = [
216
  { name = "google-genai", specifier = ">=1.14.0" },
217
  { name = "httpx", specifier = ">=0.28.1" },
218
  { name = "pydantic", specifier = ">=2.11.4" },
 
219
  { name = "python-fasthtml", specifier = ">=0.12.15" },
220
  { name = "streamlit", specifier = ">=1.45.0" },
221
  { name = "tenacity", specifier = ">=9.1.2" },
@@ -800,6 +802,20 @@ wheels = [
800
  { url = "https://files.pythonhosted.org/packages/6f/9a/e73262f6c6656262b5fdd723ad90f518f579b7bc8622e43a942eec53c938/pydantic_core-2.33.2-cp313-cp313t-win_amd64.whl", hash = "sha256:c2fc0a768ef76c15ab9238afa6da7f69895bb5d1ee83aeea2e3509af4472d0b9", size = 1935777, upload-time = "2025-04-23T18:32:25.088Z" },
801
  ]
802
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
803
  [[package]]
804
  name = "pydeck"
805
  version = "0.9.1"
 
205
  { name = "google-genai" },
206
  { name = "httpx" },
207
  { name = "pydantic" },
208
+ { name = "pydantic-settings" },
209
  { name = "python-fasthtml" },
210
  { name = "streamlit" },
211
  { name = "tenacity" },
 
217
  { name = "google-genai", specifier = ">=1.14.0" },
218
  { name = "httpx", specifier = ">=0.28.1" },
219
  { name = "pydantic", specifier = ">=2.11.4" },
220
+ { name = "pydantic-settings", specifier = ">=2.9.1" },
221
  { name = "python-fasthtml", specifier = ">=0.12.15" },
222
  { name = "streamlit", specifier = ">=1.45.0" },
223
  { name = "tenacity", specifier = ">=9.1.2" },
 
802
  { url = "https://files.pythonhosted.org/packages/6f/9a/e73262f6c6656262b5fdd723ad90f518f579b7bc8622e43a942eec53c938/pydantic_core-2.33.2-cp313-cp313t-win_amd64.whl", hash = "sha256:c2fc0a768ef76c15ab9238afa6da7f69895bb5d1ee83aeea2e3509af4472d0b9", size = 1935777, upload-time = "2025-04-23T18:32:25.088Z" },
803
  ]
804
 
805
+ [[package]]
806
+ name = "pydantic-settings"
807
+ version = "2.9.1"
808
+ source = { registry = "https://pypi.org/simple" }
809
+ dependencies = [
810
+ { name = "pydantic" },
811
+ { name = "python-dotenv" },
812
+ { name = "typing-inspection" },
813
+ ]
814
+ sdist = { url = "https://files.pythonhosted.org/packages/67/1d/42628a2c33e93f8e9acbde0d5d735fa0850f3e6a2f8cb1eb6c40b9a732ac/pydantic_settings-2.9.1.tar.gz", hash = "sha256:c509bf79d27563add44e8446233359004ed85066cd096d8b510f715e6ef5d268", size = 163234, upload-time = "2025-04-18T16:44:48.265Z" }
815
+ wheels = [
816
+ { url = "https://files.pythonhosted.org/packages/b6/5f/d6d641b490fd3ec2c4c13b4244d68deea3a1b970a97be64f34fb5504ff72/pydantic_settings-2.9.1-py3-none-any.whl", hash = "sha256:59b4f431b1defb26fe620c71a7d3968a710d719f5f4cdbbdb7926edeb770f6ef", size = 44356, upload-time = "2025-04-18T16:44:46.617Z" },
817
+ ]
818
+
819
  [[package]]
820
  name = "pydeck"
821
  version = "0.9.1"