fsal commited on
Commit
c8ebe28
0 Parent(s):

first commit

Browse files
Files changed (42) hide show
  1. .dockerignore +14 -0
  2. .env-example +22 -0
  3. .github/ISSUE_TEMPLATE/bug_report.md +38 -0
  4. .github/ISSUE_TEMPLATE/feature_request.md +17 -0
  5. .github/dependabot.yml +15 -0
  6. .github/pull_request_template.md +13 -0
  7. .github/workflows/ai_changelog.yml +48 -0
  8. .github/workflows/bumpver.yml +40 -0
  9. .github/workflows/check-file-size-limit.yml +14 -0
  10. .github/workflows/docker-hub.yml +31 -0
  11. .github/workflows/hf-space.yml +24 -0
  12. .gitignore +90 -0
  13. .idea/.gitignore +8 -0
  14. .idea/.name +1 -0
  15. .idea/inspectionProfiles/Project_Default.xml +21 -0
  16. .idea/inspectionProfiles/profiles_settings.xml +6 -0
  17. .idea/kubernetes-settings.xml +6 -0
  18. .idea/langchain-streamlit-demo.iml +10 -0
  19. .idea/misc.xml +10 -0
  20. .idea/modules.xml +8 -0
  21. .pre-commit-config.yaml +61 -0
  22. AI_CHANGELOG.md +536 -0
  23. Dockerfile +19 -0
  24. LICENSE +9 -0
  25. README.md +112 -0
  26. bumpver.toml +18 -0
  27. docker-compose.yml +16 -0
  28. kubernetes/deploy.sh +19 -0
  29. kubernetes/resources.yaml +118 -0
  30. langchain-streamlit-demo/.streamlit/config.toml +6 -0
  31. langchain-streamlit-demo/app.py +627 -0
  32. langchain-streamlit-demo/defaults.py +134 -0
  33. langchain-streamlit-demo/llm_resources.py +294 -0
  34. langchain-streamlit-demo/python_coder.py +193 -0
  35. langchain-streamlit-demo/qagen.py +72 -0
  36. langchain-streamlit-demo/research_assistant/__init__.py +3 -0
  37. langchain-streamlit-demo/research_assistant/chain.py +18 -0
  38. langchain-streamlit-demo/research_assistant/search/__init__.py +0 -0
  39. langchain-streamlit-demo/research_assistant/search/web.py +178 -0
  40. langchain-streamlit-demo/research_assistant/writer.py +78 -0
  41. langchain-streamlit-demo/summarize.py +66 -0
  42. requirements.txt +23 -0
.dockerignore ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .env
2
+ .env-example
3
+ .git/
4
+ .github
5
+ .gitignore
6
+ .idea
7
+ .mypy_cache
8
+ .pre-commit-config.yaml
9
+ .ruff_cache
10
+ Dockerfile
11
+ kubernetes
12
+ docker-compose.yml
13
+ junk/
14
+ kubernetes/
.env-example ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ APP_PORT=7860
2
+
3
+ LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
4
+ LANGCHAIN_API_KEY="ls__..."
5
+ LANGCHAIN_TRACING_V2="true"
6
+ LANGCHAIN_PROJECT="streamlit_test"
7
+
8
+ ANYSCALE_API_KEY="secret_..."
9
+ OPENAI_API_KEY="sk-..."
10
+ ANTHROPIC_API_KEY="sk-ant-..."
11
+
12
+ DEFAULT_SYSTEM_PROMPT="You are a helpful chatbot."
13
+
14
+ DEFAULT_MODEL="gpt-3.5-turbo"
15
+
16
+ DEFAULT_TEMPERATURE=0.7
17
+ MIN_TEMPERATURE=0.0
18
+ MAX_TEMPERATURE=1.0
19
+
20
+ DEFAULT_MAX_TOKENS=1000
21
+ MIN_MAX_TOKENS=1
22
+ MAX_MAX_TOKENS=100000
.github/ISSUE_TEMPLATE/bug_report.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
.github/ISSUE_TEMPLATE/feature_request.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: enhancement
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the solution you'd like**
11
+ A clear and concise description of what you want to happen.
12
+
13
+ **Describe alternatives you've considered**
14
+ A clear and concise description of any alternative solutions or features you've considered.
15
+
16
+ **Additional context**
17
+ Add any other context or screenshots about the feature request here.
.github/dependabot.yml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # To get started with Dependabot version updates, you'll need to specify which
2
+ # package ecosystems to update and where the package manifests are located.
3
+ # Please see the documentation for all configuration options:
4
+ # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5
+
6
+ version: 2
7
+ updates:
8
+ - package-ecosystem: "pip" # See documentation for possible values
9
+ directory: "/" # Location of package manifests
10
+ schedule:
11
+ interval: "weekly"
12
+ groups:
13
+ app:
14
+ patterns:
15
+ - "*"
.github/pull_request_template.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Thank you for contributing!
2
+ Before submitting this PR, please make sure:
3
+
4
+ - [ ] Your code builds clean without any errors or warnings
5
+ - [ ] Your code doesn't break anything we can't fix
6
+ - [ ] You have added appropriate tests
7
+
8
+ Please check one or more of the following to describe the nature of this PR:
9
+
10
+ - [ ] New feature
11
+ - [ ] Bug fix
12
+ - [ ] Documentation
13
+ - [ ] Other
.github/workflows/ai_changelog.yml ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Update AI Changelog on Push to Main
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ paths-ignore:
7
+ - "AI_CHANGELOG.md"
8
+
9
+ jobs:
10
+ update-changelog:
11
+ runs-on: ubuntu-latest
12
+
13
+ permissions:
14
+ contents: write
15
+ pull-requests: write
16
+
17
+ steps:
18
+
19
+ - name: Checkout code
20
+ uses: actions/checkout@v4
21
+ with:
22
+ token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
23
+ fetch-depth: 0
24
+
25
+ - name: Set up Python
26
+ uses: actions/setup-python@v4
27
+ with:
28
+ python-version: 3.11
29
+ cache: pip
30
+
31
+ - name: Install Python libraries
32
+ run: |
33
+ pip install --user ai_changelog==0.0.9
34
+
35
+ - name: Execute script and prepend to AI_CHANGELOG.md
36
+ env:
37
+ OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
38
+ LANGCHAIN_API_KEY: ${{ secrets.LANGCHAIN_API_KEY }}
39
+ LANGCHAIN_PROJECT: ai-changelog-langchain-streamlit-demo
40
+ LANGCHAIN_ENDPOINT: https://api.smith.langchain.com
41
+ LANGCHAIN_TRACING_V2: true
42
+ run: ai_changelog origin/main^..origin/main
43
+
44
+ - name: Commit changes
45
+ with:
46
+ COMMIT_MESSAGE: "Update AI Changelog"
47
+ file_pattern: "AI_CHANGELOG.md"
48
+ uses: stefanzweifel/git-auto-commit-action@v4
.github/workflows/bumpver.yml ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Bump Version
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ bump:
7
+ type: choice
8
+ description: 'Bump major, minor, or patch version'
9
+ required: true
10
+ default: 'patch'
11
+ options:
12
+ - 'major'
13
+ - 'minor'
14
+ - 'patch'
15
+
16
+ jobs:
17
+ bump-version:
18
+ runs-on: ubuntu-latest
19
+ permissions:
20
+ contents: write
21
+ steps:
22
+ - name: Checkout code
23
+ uses: actions/checkout@v4
24
+ with:
25
+ token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
26
+ fetch-depth: 0
27
+ - name: Set up Python
28
+ uses: actions/setup-python@v4
29
+ with:
30
+ python-version: 3.11
31
+ cache: pip
32
+ - name: Install Python libraries
33
+ run: |
34
+ pip install --user bumpver
35
+ - name: git config
36
+ run: |
37
+ git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
38
+ git config --local user.name "github-actions[bot]"
39
+ - name: Bump version
40
+ run: bumpver update --commit --tag-commit --${{ github.event.inputs.bump }} --push
.github/workflows/check-file-size-limit.yml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: 10 MB file size limit
2
+ on:
3
+ pull_request:
4
+ branches: [main]
5
+
6
+ jobs:
7
+ check-file-sizes:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - name: Check large files
11
+ uses: ActionsDesk/lfs-warning@v2.0
12
+ with:
13
+ filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces
14
+ token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
.github/workflows/docker-hub.yml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Push to Docker Hub
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '*.*.*'
7
+
8
+ jobs:
9
+ build-and-push-docker:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ with:
14
+ fetch-depth: 0
15
+ token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
16
+
17
+ - name: Log in to Docker Hub
18
+ uses: docker/login-action@v1
19
+ with:
20
+ username: joshuasundance
21
+ password: ${{ secrets.DOCKERHUB_TOKEN }}
22
+
23
+ - name: Build Docker image
24
+ run: |
25
+ docker build \
26
+ -t joshuasundance/langchain-streamlit-demo:${{ github.ref_name }} \
27
+ -t joshuasundance/langchain-streamlit-demo:latest \
28
+ .
29
+
30
+ - name: Push to Docker Hub
31
+ run: docker push -a joshuasundance/langchain-streamlit-demo
.github/workflows/hf-space.yml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Push to HuggingFace Space
2
+
3
+ on:
4
+ workflow_run:
5
+ workflows: ["Update AI Changelog on Push to Main"]
6
+ branches: [main]
7
+ types:
8
+ - completed
9
+ workflow_dispatch:
10
+
11
+ jobs:
12
+ push-to-huggingface:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ with:
17
+ fetch-depth: 0
18
+ token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
19
+
20
+ - name: Push to HuggingFace Space
21
+ env:
22
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
23
+ run: |
24
+ git push https://contentwise:$HF_TOKEN@huggingface.co/spaces/contentwise/langchain-streamlit-demo main
.gitignore ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *$py.class
2
+ *.chainlit
3
+ *.chroma
4
+ *.cover
5
+ *.egg
6
+ *.egg-info/
7
+ *.env
8
+ *.langchain.db
9
+ *.log
10
+ *.manifest
11
+ *.mo
12
+ *.pot
13
+ *.py,cover
14
+ *.py[cod]
15
+ *.sage.py
16
+ *.so
17
+ *.spec
18
+ .DS_STORE
19
+ .Python
20
+ .cache
21
+ .coverage
22
+ .coverage.*
23
+ .dmypy.json
24
+ .eggs/
25
+ .env
26
+ .hypothesis/
27
+ .installed.cfg
28
+ .ipynb_checkpoints
29
+ .mypy_cache/
30
+ .nox/
31
+ .pyre/
32
+ .pytest_cache/
33
+ .python-version
34
+ .ropeproject
35
+ .ruff_cache/
36
+ .scrapy
37
+ .spyderproject
38
+ .spyproject
39
+ .tox/
40
+ .venv
41
+ .vscode
42
+ .webassets-cache
43
+ /site
44
+ ENV/
45
+ MANIFEST
46
+ __pycache__
47
+ __pycache__/
48
+ __pypackages__/
49
+ build/
50
+ celerybeat-schedule
51
+ celerybeat.pid
52
+ coverage.xml
53
+ credentials.json
54
+ data/
55
+ db.sqlite3
56
+ db.sqlite3-journal
57
+ develop-eggs/
58
+ dist/
59
+ dmypy.json
60
+ docs/_build/
61
+ downloads/
62
+ eggs/
63
+ env.bak/
64
+ env/
65
+ fly.toml
66
+ htmlcov/
67
+ instance/
68
+ ipython_config.py
69
+ junk/
70
+ lib/
71
+ lib64/
72
+ local_settings.py
73
+ models/*.bin
74
+ nosetests.xml
75
+ notebooks/scratch/
76
+ parts/
77
+ pip-delete-this-directory.txt
78
+ pip-log.txt
79
+ pip-wheel-metadata/
80
+ profile_default/
81
+ sdist/
82
+ share/python-wheels/
83
+ storage
84
+ target/
85
+ token.json
86
+ var/
87
+ venv
88
+ venv.bak/
89
+ venv/
90
+ wheels/
.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
.idea/.name ADDED
@@ -0,0 +1 @@
 
 
1
+ langchain-streamlit-demo
.idea/inspectionProfiles/Project_Default.xml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
5
+ <inspection_tool class="InconsistentLineSeparators" enabled="true" level="WARNING" enabled_by_default="true" />
6
+ <inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
7
+ <option name="ourVersions">
8
+ <value>
9
+ <list size="5">
10
+ <item index="0" class="java.lang.String" itemvalue="3.12" />
11
+ <item index="1" class="java.lang.String" itemvalue="3.8" />
12
+ <item index="2" class="java.lang.String" itemvalue="3.9" />
13
+ <item index="3" class="java.lang.String" itemvalue="3.10" />
14
+ <item index="4" class="java.lang.String" itemvalue="3.11" />
15
+ </list>
16
+ </value>
17
+ </option>
18
+ </inspection_tool>
19
+ <inspection_tool class="PyShadowingNamesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
20
+ </profile>
21
+ </component>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/kubernetes-settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="KubernetesSettings">
4
+ <option name="contextName" value="swca-aks" />
5
+ </component>
6
+ </project>
.idea/langchain-streamlit-demo.iml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <sourceFolder url="file://$MODULE_DIR$/langchain-streamlit-demo" isTestSource="false" />
6
+ </content>
7
+ <orderEntry type="jdk" jdkName="Remote Python 3.11.4 Docker (&lt;none&gt;:&lt;none&gt;) (5)" jdkType="Python SDK" />
8
+ <orderEntry type="sourceFolder" forTests="false" />
9
+ </component>
10
+ </module>
.idea/misc.xml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="Black">
4
+ <option name="sdkName" value="Remote Python 3.11.4 Docker (&lt;none&gt;:&lt;none&gt;) (5)" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="Remote Python 3.11.4 Docker (&lt;none&gt;:&lt;none&gt;) (5)" project-jdk-type="Python SDK" />
7
+ <component name="PythonCompatibilityInspectionAdvertiser">
8
+ <option name="version" value="3" />
9
+ </component>
10
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/langchain-streamlit-demo.iml" filepath="$PROJECT_DIR$/.idea/langchain-streamlit-demo.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.pre-commit-config.yaml ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Don't know what this file is? See https://pre-commit.com/
2
+ # pip install pre-commit
3
+ # pre-commit install
4
+ # pre-commit autoupdate
5
+ # Apply to all files without commiting:
6
+ # pre-commit run --all-files
7
+ # I recommend running this until you pass all checks, and then commit.
8
+ # Fix what you need to and then let the pre-commit hooks resolve their conflicts.
9
+ # You may need to git add -u between runs.
10
+ exclude: "AI_CHANGELOG.md"
11
+ repos:
12
+ - repo: https://github.com/charliermarsh/ruff-pre-commit
13
+ rev: "v0.1.8"
14
+ hooks:
15
+ - id: ruff
16
+ args: [--fix, --exit-non-zero-on-fix, --ignore, E501]
17
+ - repo: https://github.com/koalaman/shellcheck-precommit
18
+ rev: v0.9.0
19
+ hooks:
20
+ - id: shellcheck
21
+ - repo: https://github.com/pre-commit/pre-commit-hooks
22
+ rev: v4.5.0
23
+ hooks:
24
+ - id: check-ast
25
+ - id: check-builtin-literals
26
+ - id: check-merge-conflict
27
+ - id: check-symlinks
28
+ - id: check-toml
29
+ - id: check-xml
30
+ - id: debug-statements
31
+ - id: check-case-conflict
32
+ - id: check-docstring-first
33
+ - id: check-executables-have-shebangs
34
+ - id: check-json
35
+ # - id: check-yaml
36
+ - id: debug-statements
37
+ - id: fix-byte-order-marker
38
+ - id: detect-private-key
39
+ - id: end-of-file-fixer
40
+ - id: trailing-whitespace
41
+ - id: mixed-line-ending
42
+ - id: requirements-txt-fixer
43
+ - repo: https://github.com/psf/black
44
+ rev: 23.12.0
45
+ hooks:
46
+ - id: black
47
+ - repo: https://github.com/asottile/add-trailing-comma
48
+ rev: v3.1.0
49
+ hooks:
50
+ - id: add-trailing-comma
51
+ - repo: https://github.com/pre-commit/mirrors-mypy
52
+ rev: v1.7.1
53
+ hooks:
54
+ - id: mypy
55
+ additional_dependencies:
56
+ - types-requests
57
+ - repo: https://github.com/PyCQA/bandit
58
+ rev: 1.7.6
59
+ hooks:
60
+ - id: bandit
61
+ args: ["-x", "tests/*.py", "-s", "B113"]
AI_CHANGELOG.md ADDED
@@ -0,0 +1,536 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI CHANGELOG
2
+ ## [Updated Python dependencies](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/511218395a883d498a66e4c136be25c110434b2a)
3
+ Mon Jan 29 12:14:25 2024 +0000
4
+ - The Python dependencies in the requirements.txt file have been updated. The following packages were upgraded: anthropic, black, duckduckgo-search, langchain, langsmith, numexpr, openai, and pypdf.
5
+ ## [Updated langchain package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/997c45707653d2b35465402714e289b56033f1e7)
6
+ Mon Jan 1 13:05:22 2024 +0000
7
+ - The langchain package version in the requirements.txt file has been updated from 0.0.352 to 0.0.353.
8
+ ## [Refactor and improve documentation for assistant tools](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/bd1f3d1c1d1c34d960051bdd37370ddba520b665)
9
+ Wed Dec 27 19:28:06 2023 -0500
10
+ - This commit includes several changes to improve the structure and documentation of the assistant tools used in the Streamlit app. The changes include:
11
+ - 1. Reordering of import statements for better readability and organization.
12
+ - 2. Refactoring of the research_assistant_tool, python_coder_tool, doc_chain_tool, and doc_question_tool functions to improve their documentation. The new documentation provides clearer instructions for use and sets more explicit expectations for the type of input these tools should receive.
13
+ - 3. Adjustments to the order of tools in the TOOLS list to ensure the doc_question_tool is prioritized.
14
+ ## [Updated application version to 2.1.4](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/13a5813ecd2f6bdfbc39bd2c50bdd36c43162733)
15
+ Wed Dec 27 16:25:25 2023 +0000
16
+ - The application version has been updated from 2.1.3 to 2.1.4 in multiple files: bumpver.toml, resources.yaml, and app.py. This change includes updating the current_version in bumpver.toml, the Docker image tag in the Kubernetes resources file, and the __version__ attribute in the app.py file.
17
+ ## [Updated chat input formatting in python_coder.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/39161d38df2e230a22fcc7b1eda31e486d989728)
18
+ Wed Dec 27 11:22:28 2023 -0500
19
+ - This commit changes the way user input is displayed in the chat history. Previously, a colon was prepended to user input. This has been removed to improve readability.
20
+ ## [Refactor Tool creation and usage in langchain-streamlit-demo](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f4b7ac3592a6b55c09bf3b2c7692467410d63687)
21
+ Wed Dec 27 11:21:00 2023 -0500
22
+ - This commit refactors the way tools are created and used within the langchain-streamlit-demo. The 'Tool' class has been replaced by a 'tool' decorator, making the code more Pythonic and easier to read.
23
+ - The 'research_assistant_tool', 'python_coder_tool', 'doc_chain_tool', and 'doc_question_tool' have all been updated to use this new decorator. This change also allows for the 'callbacks' parameter to be passed directly to the tool functions, improving flexibility.
24
+ - The 'description' attribute of each tool has been moved inside the function as a docstring, providing context about what each tool does in a more standard Python way.
25
+ ## [Changed default value of 'strict' parameter in check_mypy function](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/238a2a143b60d9ed340de283864a99f99bd0b0bc)
26
+ Wed Dec 27 10:54:14 2023 -0500
27
+ - The 'strict' parameter in the 'check_mypy' function has been modified. Previously, the default value was set to 'True'. This commit changes the default value to 'False'. This change affects how mypy checks are run on a file.
28
+ ## [Extended subprocess timeouts and added no-cache option to ruff commands](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f642d160e47dd94a51437ab57aa4757aabd013b6)
29
+ Wed Dec 27 10:53:23 2023 -0500
30
+ - Increased the timeout for subprocess commands from 3 seconds to 30 seconds to allow for longer processes. Added the '--no-cache' option to all 'ruff' commands to ensure that the most recent version of the file is being checked and formatted. Additionally, imported 'MessagesPlaceholder' from 'langchain.prompts' and added it to the 'ChatPromptTemplate' to include the chat history in the prompt.
31
+ ## [Added ConversationBufferMemory to agent](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/bc3a159a9e104625425de5578e0e060f12e6fe83)
32
+ Wed Dec 27 10:40:41 2023 -0500
33
+ - The ConversationBufferMemory was imported from the langchain.memory module and added to the agent. This change will enable the agent to store the conversation history, allowing it to respond more accurately to user inputs based on previous interactions.
34
+ ## [Bumped application version from 2.1.2 to 2.1.3](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/4c0a3bf81f9ddf898312804522c6b3f084bfa289)
35
+ Wed Dec 27 04:30:24 2023 +0000
36
+ - The 'current_version' field in 'bumpver.toml' was updated from 2.1.2 to 2.1.3, indicating a new version of the application.
37
+ - In 'kubernetes/resources.yaml', the image version for 'langchain-streamlit-demo' container was updated to match the new version.
38
+ - The '__version__' variable in 'app.py' under 'langchain-streamlit-demo' was also updated to reflect the new version.
39
+ ## [Added git configuration to GitHub Actions workflow](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/090173a3ba6868f868a9b707d657b4f180537c80)
40
+ Tue Dec 26 23:28:58 2023 -0500
41
+ - This commit introduces changes to the GitHub Actions workflow. Specifically, it adds a git configuration step to set the user email and name to 'github-actions[bot]'. This is important as it ensures that the bot is recognized as the author of any commits made during the workflow process.
42
+ ## [Added choice type to bump input in GitHub Actions workflow](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/414e3d472d9eda092b0fde7ee76d22fb033b837d)
43
+ Tue Dec 26 23:12:25 2023 -0500
44
+ - This commit modifies the 'bumpver.yml' file in our GitHub Actions workflow. Specifically, it adds a 'choice' type to the 'bump' input. This change ensures that the user can only select from predefined options when triggering the version bump workflow, thus minimizing the chance of errors.
45
+ ## [Added GitHub Actions Workflow for Version Bumping](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/6a47e1364d6f23b201ed7ae277698ada08793617)
46
+ Tue Dec 26 23:11:03 2023 -0500
47
+ - This commit introduces a new GitHub Actions workflow named 'Bump Version'.
48
+ - The workflow can be manually triggered and requires an input to specify whether to bump the major, minor, or patch version of the project.
49
+ - The job within the workflow runs on the latest Ubuntu, checks out the code, sets up Python 3.11, installs the 'bumpver' Python library, and then uses this library to bump the specified version.
50
+ - The updated version is then committed and tagged, and the changes are pushed back to the repository.
51
+ ## [Updated package versions in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/cd6528dd9a52049f0551411c148a7e7b0d3af0e9)
52
+ Mon Dec 25 12:11:03 2023 +0000
53
+ - This commit updates the versions of several packages in the requirements.txt file. The packages updated include: anthropic, black, duckduckgo-search, langchain, langsmith, mypy, openai, and pypdf. The purpose of these updates is to keep the project's dependencies up-to-date with the latest versions.
54
+ ## [Enabled config callbacks and updated tool descriptions](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/aa82c149b06608f987e7f8ff3882ed4fc53ed6e6)
55
+ Tue Dec 19 16:18:20 2023 -0600
56
+ - The commit enables the `get_config(callbacks)` within the `doc_chain_tool` and `doc_question_tool` functions. This allows the functions to use the specified config callbacks.
57
+ - It also updates the description of the `doc_question_tool` to clarify its usage and warn users not to send anything other than a question as it already has context.
58
+ ## [Updated versions of pre-commit hooks](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/7c729a3108c70efd5707ec4631d55e67f2968f8e)
59
+ Tue Dec 19 16:43:48 2023 -0500
60
+ - This commit updates the versions of multiple pre-commit hooks. The updated hooks include 'ruff-pre-commit' (from v0.0.291 to v0.1.8), 'pre-commit-hooks' (from v4.4.0 to v4.5.0), 'black' (from 23.9.1 to 23.12.0), 'mirrors-mypy' (from v1.5.1 to v1.7.1), and 'bandit' (from 1.7.5 to 1.7.6).
61
+ - These updates ensure that the project is using the most recent and secure versions of these tools, which can help to catch more potential issues during the pre-commit phase.
62
+ ## [Removed vcs.xml and updated default agent type in llm_resources.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/71e9279d36d4c78c6b18956c598445f6caf2ced6)
63
+ Tue Dec 19 16:10:18 2023 -0500
64
+ - The vcs.xml file, which was used for version control settings in the IDE, has been deleted. This indicates a change in the version control system or its configuration.
65
+ - In the llm_resources.py file, the default agent type for the get_doc_agent function has been changed from STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION to CHAT_ZERO_SHOT_REACT_DESCRIPTION. This suggests a change in how the agent responds in chat scenarios.
66
+ ## [Updated several package versions in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/1cf958abefb5cd3408fac7c64d96ecf6f3d62a28)
67
+ Tue Dec 19 18:26:07 2023 +0000
68
+ - The versions of the following packages were updated: anthropic (from 0.7.8 to 0.8.0), duckduckgo-search (from 4.0.0 to 4.1.0), langchain (from 0.0.350 to 0.0.351), and langsmith (from 0.0.71 to 0.0.72).
69
+ ## [Introduced document-agent and enhanced user-document-chat](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f88e711dc42403046604176f8eaa3ec716572cd7)
70
+ Tue Dec 19 12:12:02 2023 -0600
71
+ - This commit introduces the 'document-agent' tool which uses the 'doc_chain_agent' to return a response based on the user's custom context. It is recommended to use this tool when the user's question is complex, as it attempts to break it down into simpler questions.
72
+ - The 'user-document-chat' tool has been enhanced to return a response based on the user's custom context. It is advised to use this tool before conducting web research.
73
+ - A new function 'get_doc_agent' has been added in the 'llm_resources.py' file. This function initializes an agent with the given tools and a language model. The agent assists a chatbot with answering questions about a document. It breaks up incoming questions into multiple parts, and uses the tools provided to answer smaller questions before answering the larger question.
74
+ ## [Updated Dependabot configuration](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/4e2d2fd2be93bc022093064214e372833debd235)
75
+ Tue Dec 19 13:24:43 2023 -0500
76
+ - Added a new group 'app' in the Dependabot configuration file to include all dependencies ('*'). This will group updates for all dependencies together.
77
+ ## [Refactor Memory Management and Remove Intermediate Steps](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3a2b820bcfb7ef8dc4157626de454be9267e1e91)
78
+ Tue Dec 19 09:35:23 2023 -0500
79
+ - This commit refactors the memory management in the Streamlit app and other resources. It replaces the 'AgentTokenBufferMemory' with 'ConversationBufferMemory' for a more efficient memory usage. The 'return_messages' flag is set to 'True' in the new memory management system.
80
+ - The commit also disables the return of intermediate steps in both the Streamlit app and agent execution by setting 'return_intermediate_steps' to 'False'. This change can help to improve performance by reducing unnecessary computations.
81
+ - Furthermore, the commit simplifies the 'python_coder.py' script by removing unnecessary imports and argument parsing related to 'black', 'ruff', and 'mypy'. It also removes the main execution block, indicating that this script may now be used as a module rather than a standalone script.
82
+ - The changes in this commit aim to improve performance and maintainability of the code.
83
+ ## [Updated application version to 2.1.2](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3fddfc9d34f66f92255f6a0d0878e20db401d694)
84
+ Mon Dec 18 11:02:13 2023 -0500
85
+ - Bumped the current version of the application in the bumpver.toml file from 2.1.1 to 2.1.2.
86
+ - Updated the version of the Docker image used in the Kubernetes resources.yaml file from 2.1.1 to 2.1.2.
87
+ - Changed the __version__ variable in the app.py file of the langchain-streamlit-demo application from 2.1.1 to 2.1.2.
88
+ ## [Added PythonCompatibilityInspectionAdvertiser to misc.xml and updated agent_type in python_coder.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/04cf693d125a71f61b12ce8f5f8024c79fe9908c)
89
+ Mon Dec 18 10:59:56 2023 -0500
90
+ - In the misc.xml file, a new component 'PythonCompatibilityInspectionAdvertiser' was added to the project. This component is used for inspecting python compatibility.
91
+ - In the python_coder.py file, the default value for the 'agent_type' argument in the 'get_agent' function was changed from 'OPENAI_FUNCTIONS' to 'CHAT_CONVERSATIONAL_REACT_DESCRIPTION'. This change affects the type of agent that is initialized by default when the 'get_agent' function is called.
92
+ ## [Bumped version from 2.1.0 to 2.1.1](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/5a348f9029a30b49eda60ed7b6d612f9602c8bc8)
93
+ Mon Dec 18 10:41:42 2023 -0500
94
+ - The current version number in bumpver.toml, the image tag in kubernetes/resources.yaml, and the __version__ variable in langchain-streamlit-demo/app.py have all been updated from 2.1.0 to 2.1.1.
95
+ ## [Update PyPDF package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/fc63706a675b02d7b7db5bb31b81d14da3f17bed)
96
+ Mon Dec 18 13:16:53 2023 +0000
97
+ - This commit updates the version of the PyPDF package from 3.17.2 to 3.17.3 in the requirements.txt file.
98
+ ## [Updated langsmith package](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/688f2b15a1d6086d6c81ae06f27495421641ac20)
99
+ Mon Dec 18 13:15:37 2023 +0000
100
+ - The langsmith package has been updated from version 0.0.69 to 0.0.71 in the requirements.txt file.
101
+ ## [Updated anthropic package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d0fdfb5eaa95b1628140a0a1650d416220070fb4)
102
+ Mon Dec 18 13:15:24 2023 +0000
103
+ - The anthropic package in the requirements.txt file has been updated from version 0.7.7 to 0.7.8. No other packages were affected in this commit.
104
+ ## [Updated OpenAI library version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/4c03ff5ea6f5c3097a53e0c0c5db36967d5e1e8f)
105
+ Mon Dec 18 13:13:17 2023 +0000
106
+ - The OpenAI library version in requirements.txt has been updated from 1.3.8 to 1.5.0. This update may include new features, bug fixes, and performance improvements.
107
+ ## [Updated langchain version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/00528c803be2d7b84bedf1d5469d269529e8a3db)
108
+ Mon Dec 18 12:13:48 2023 +0000
109
+ - The langchain package was updated from version 0.0.348 to 0.0.350 in the requirements.txt file. This change was necessary to incorporate the latest features and bug fixes from the langchain package.
110
+ ## [Updated application version to 2.1.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/6a5206daa0170076d2e55f3878204571c03b0dbe)
111
+ Wed Dec 13 17:57:20 2023 -0500
112
+ - The version of the application has been updated from 2.0.1 to 2.1.0. This change is reflected in the bumpver.toml file, which tracks the current version of the application.
113
+ - In addition, the Docker image referenced in the Kubernetes resource configuration file (resources.yaml) has been updated to match the new version.
114
+ - Finally, the version number displayed on the application's Streamlit page (app.py) has also been updated to reflect the new version.
115
+ ## [Added Python Coder Assistant to Application Tools](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/94f9b82dcf0b254853e489743d140c674acb2bb6)
116
+ Wed Dec 13 17:54:48 2023 -0500
117
+ - In this commit, a Python Coder Assistant was added to the application's tools. This assistant is capable of writing Python code given clear instructions and requirements. It was implemented by importing the 'get_agent' function from the newly created 'python_coder.py' file and adding the resulting 'python_coder_tool' to the 'TOOLS' list.
118
+ - The 'python_coder.py' file contains the necessary functions and classes to initialize the Python Coder Assistant, check the code, and submit the code. It also ensures that the code conforms to the black, ruff, and strict mypy standards.
119
+ - The 'requirements.txt' file was updated to include the new dependencies required by the Python Coder Assistant, namely 'black', 'mypy', and 'ruff'.
120
+ ## [Added certifi and requests packages to requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/920ffd4c80beac16450c762c3caab609ede20fd0)
121
+ Wed Dec 13 18:16:18 2023 +0000
122
+ - The certifi and requests packages were added to the requirements.txt file to avoid potential vulnerabilities. These packages were not directly required, but were pinned by Snyk for security reasons.
123
+ ## [Enhanced tool descriptions and added 'llm-math' tool](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/13d332463cb60b873e75f45f783ca23cef55de93)
124
+ Wed Dec 13 13:09:22 2023 -0500
125
+ - The commit enhances the descriptions of the 'web-research-assistant' and 'user-document-chat' tools. It also adds the 'llm-math' tool to the default tools list in the 'app.py' file of the 'langchain-streamlit-demo' project.
126
+ - In the 'requirements.txt' file, the 'numexpr' package (version 2.8.8) has been added.
127
+ ## [Added tool loading functionality and updated assistant description](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d8714b091e4b40041ac26cd8a2854e5ef34011cb)
128
+ Wed Dec 13 10:35:50 2023 -0500
129
+ - In this commit, the developer has added a line to load additional tools using the 'load_tools' function from the 'langchain.agents' module. This extends the functionality of the default tools available in the application.
130
+ - Additionally, the developer has updated the description of the 'web-research-assistant' tool to provide more context on its usage and cost implications. The updated description advises users to use the assistant sparingly due to its relative expense and suggests using DuckDuckGo for quick facts instead.
131
+ ## [Bumped version from 2.0.0 to 2.0.1](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/9e698f4a274c4780d013fdfb0110a1f7675b580e)
132
+ Wed Dec 13 14:10:39 2023 +0000
133
+ - This commit updates the version number across several files. Specifically, it modifies the current version in bumpver.toml, the image version in the Kubernetes resources.yaml, and the __version__ variable in the langchain-streamlit-demo/app.py. All these changes reflect the version bump from 2.0.0 to 2.0.1.
134
+ ## [Improved chat message display in Streamlit app](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f15299ef4c3f6d17e1fed55329d9be2683e3e6de)
135
+ Wed Dec 13 09:08:41 2023 -0500
136
+ - This commit updates the chat history section of the Streamlit app. It adds a check to ensure that a message is only displayed if it has content and is of a recognized type ('ai', 'assistant', 'human', 'user'). This prevents empty or unhandled message types from being displayed, improving the user experience.
137
+ ## [Update version to 2.0.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/870f9c796ca452f131df63efeb5fd22f41b35cb3)
138
+ Tue Dec 12 17:23:25 2023 -0500
139
+ - The version number in bumpver.toml, kubernetes resources.yaml, and app.py has been updated from 1.1.0 to 2.0.0. This includes updates to the current version in the bumpver configuration, the docker image version in the Kubernetes resources file, and the application version in the app.py file.
140
+ ## [Added new search tools and updated assistant descriptions](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/1ea3b530f16118973e99729685e5e0be621e8a91)
141
+ Tue Dec 12 17:03:20 2023 -0500
142
+ - Added DuckDuckGoSearchRun and WikipediaQueryRun to the default tools. Updated the description of 'web-research-assistant' to mention quick facts usage of DuckDuckGo. Also, added these tools to the TOOLS list in the provider's condition.
143
+ - Updated the DEFAULT_SYSTEM_PROMPT in defaults.py to emphasize a step-by-step approach.
144
+ - Added 'wikipedia==1.4.0' to the requirements.txt file for using the Wikipedia API.
145
+ ## [Updated pre-commit hooks and refactored Streamlit app](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/5825ff944afc97081a1cc740d0ccf3095a80d697)
146
+ Tue Dec 12 16:03:57 2023 -0500
147
+ - 1. Updated the pre-commit configuration file to reorder the hooks, replacing the 'mypy' hook with the 'black' hook and vice versa.
148
+ - 2. Refactored the Streamlit application 'app.py' by adding a new import statement and restructuring the code for better readability and performance. This includes changes in the way the 'research_assistant_tool' and 'doc_chain_tool' are defined and used, and the addition of the 'get_config' function for creating a configuration dictionary.
149
+ - 3. Updated the 'llm_resources.py' file to include 'callbacks' as an argument in the 'get_agent' function, and removed the 'create_retriever_tool' usage from the 'get_runnable' function.
150
+ ## [Added TODO comments for future development](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/547d578fa19e7542737e6165e21b93c0369f5f4f)
151
+ Tue Dec 12 14:50:49 2023 -0500
152
+ - This commit includes TODO comments in the app.py file of the langchain-streamlit-demo. These comments outline future development tasks related to the usage of agents in the OpenAI or Azure OpenAI, the usage of runnable, and the addition of these to tools.
153
+ ## [Added web research assistant and updated dependencies](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/883f3bee29ccd1f670e48245d3ada8d1fd7fd3c9)
154
+ Tue Dec 12 14:48:04 2023 -0500
155
+ - Added a web research assistant that uses DuckDuckGo's search API to perform web searches and BeautifulSoup to scrape text from the resulting URLs. The assistant then generates a report based on the scraped information.
156
+ - Updated the pre-commit configuration to add a dependency on 'types-requests'.
157
+ - Refactored the 'get_runnable' function in 'llm_resources.py' to use the new 'get_agent' function, which creates an agent with a set of tools and a chat history.
158
+ - Updated 'app.py' to use the new web research assistant, and refactored the code to use the new 'get_agent' function.
159
+ - Updated the requirements file to include 'beautifulsoup4' and 'duckduckgo-search'.
160
+ ## [Enhanced chat functionality in the LLMChain](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/048798b27efb2d8dbe52648fdcf34176a608a296)
161
+ Tue Dec 12 14:01:20 2023 -0500
162
+ - This commit introduces an enhancement to the chat functionality in the LLMChain. It does so by incorporating the chat history into the get_runnable function. The commit also restructures the import statements in the llm_resources.py file and adds new imports necessary for the enhancement.
163
+ - The get_runnable function now takes an additional parameter 'chat_history'. It creates a retriever tool and an OpenAIFunctionsAgent with a prompt that includes system messages and the chat history. An AgentTokenBufferMemory is also initialized with the chat history.
164
+ - The agent and the tools are then executed using an AgentExecutor that returns the output of the conversation. This enhancement allows the chatbot to maintain a history of the conversation and use it to provide more context-aware responses.
165
+ ## [Updated version from 1.0.3 to 1.1.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f9ace4d1dc84d8acfab5d95c489b461362d680d0)
166
+ Mon Dec 11 12:36:57 2023 -0500
167
+ - The version number in bumpver.toml, kubernetes/resources.yaml, and langchain-streamlit-demo/app.py has been updated from 1.0.3 to 1.1.0.
168
+ - In bumpver.toml, the current_version key's value is updated.
169
+ - In kubernetes/resources.yaml, the image version of the langchain-streamlit-demo container is updated.
170
+ - In langchain-streamlit-demo/app.py, the __version__ variable is updated.
171
+ ## [Removed BM25Retriever and related dependencies](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/c132355839f407ff11494aeee4ff361a7646e913)
172
+ Mon Dec 11 12:31:16 2023 -0500
173
+ - In this commit, the use of the BM25Retriever and its related dependencies were removed from the project. This included changes in the 'app.py' and 'llm_resources.py' files where the retriever was being used. The 'get_texts_and_retriever' function was removed from 'llm_resources.py' as it was no longer needed. Additionally, the 'rank_bm25' library was removed from the project's requirements in the 'requirements.txt' file.
174
+ ## [Updated retriever function and imports in app.py and llm_resources.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/e8b1107dfef3f5b1bb104b9e4d1f98095a2228ff)
175
+ Mon Dec 11 10:27:19 2023 -0600
176
+ - In the app.py file, the import statement was updated to include the 'get_texts_and_multiretriever' function from the 'llm_resources' module. The function 'get_texts_and_retriever_cacheable_wrapper' was also updated to call 'get_texts_and_multiretriever' instead of 'get_texts_and_retriever'.
177
+ - In the llm_resources.py file, the function 'get_texts_and_retriever2' was renamed to 'get_texts_and_multiretriever'.
178
+ ## [Updated OpenAI package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/cc740341e378b46896457ff83048b981ff8eadc2)
179
+ Mon Dec 11 12:44:43 2023 +0000
180
+ - The OpenAI package version in requirements.txt was updated from 1.3.7 to 1.3.8.
181
+ ## [Updated pypdf version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d880411e5d768b13efb25d3d7ac4ead6755a4ff0)
182
+ Mon Dec 11 12:44:36 2023 +0000
183
+ - The pypdf package version in requirements.txt was updated from 3.17.1 to 3.17.2. This change doesn't affect any other dependencies.
184
+ ## [Updated langchain version in requirements](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f23ab029e9e13452f9fe0e41ed48f36f257cdbfb)
185
+ Mon Dec 11 12:44:32 2023 +0000
186
+ - The version of langchain in the requirements.txt file was updated from 0.0.345 to 0.0.348. No other changes were made.
187
+ ## [Added new retriever function and updated langchain version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/09bee796cc0e56bf167cff0d12cbdb233184c376)
188
+ Fri Dec 8 15:20:41 2023 -0600
189
+ - In the langchain-streamlit-demo/llm_resources.py file, a new function called get_texts_and_retriever2 was added. This function processes uploaded file bytes and returns a list of documents and a BaseRetriever instance. It introduces new retriever classes MultiVectorRetriever and MultiQueryRetriever, and uses InMemoryStore for storage. Also, it generates unique identifiers for each text using the uuid library.
190
+ - In the requirements.txt file, the version of the langchain package was updated from 0.0.345 to 0.0.346.
191
+ ## [Updated Azure endpoint configuration in embeddings_kwargs](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/56dd4e0c90ab9ec09be58c184e6bf100398c9be8)
192
+ Thu Dec 7 11:00:31 2023 -0500
193
+ - This commit modifies the 'get_texts_and_retriever' function in the 'llm_resources.py' file.
194
+ - If 'use_azure' is True and 'azure_kwargs' is provided, the 'azure_endpoint' is now set with the value from 'openai_api_base'.
195
+ - This update ensures that the correct Azure endpoint is used when creating AzureOpenAIEmbeddings.
196
+ ## [Bumped version from 1.0.2 to 1.0.3](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/1c0c6c5c2e58321a2dd5f58c079c070d601e315c)
197
+ Thu Dec 7 10:25:07 2023 -0500
198
+ - Updated the version number in bumpver.toml, the Docker image version in the Kubernetes resources.yaml, and the application version in the app.py file of the langchain-streamlit-demo.
199
+ ## [Refactored variable name in AzureChatOpenAI constructor](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/17f45e34541f6182f43665bf6c0fe66b2a725eb8)
200
+ Thu Dec 7 10:21:30 2023 -0500
201
+ - Changed the variable name 'openai_api_base' to 'azure_endpoint' in the AzureChatOpenAI constructor for clarity and consistency.
202
+ ## [Update version from 1.0.1 to 1.0.2](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/8fee298d3553974031cad83e42e34b6559df5fe5)
203
+ Mon Dec 4 11:27:10 2023 -0500
204
+ - The version number has been updated in the following files: `bumpver.toml` (the project's version control file), `kubernetes/resources.yaml` (the Kubernetes resources configuration file), and `langchain-streamlit-demo/app.py` (the application's main Python file).
205
+ - In `bumpver.toml`, the `current_version` variable has been updated to `1.0.2`.
206
+ - In `kubernetes/resources.yaml`, the Docker image version has been updated to `1.0.2`.
207
+ - In `langchain-streamlit-demo/app.py`, the `__version__` variable has been updated to `1.0.2`.
208
+ ## [Updated package versions in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3d7adb3b01d4565d59e4b999c69e2d554fab9f02)
209
+ Mon Dec 4 11:11:37 2023 -0500
210
+ - This commit updates the versions of several packages in the requirements.txt file. The updated packages include 'langchain', 'langsmith', 'openai', 'streamlit', and 'tiktoken'.
211
+ - Such updates are necessary to keep the software up-to-date with the latest improvements, bug fixes, and security patches in the packages that it depends on.
212
+ ## [Updated version to 1.0.1](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d15b0d2767d9647dd0b05649e5c1d008fa56920e)
213
+ Thu Nov 30 15:19:39 2023 -0500
214
+ - The version number in bumpver.toml, the Docker image tag in kubernetes/resources.yaml and the version in langchain-streamlit-demo/app.py have been updated from 1.0.0 to 1.0.1.
215
+ ## [Refactored error handling and added support for AzureOpenAIEmbeddings](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/ead1471ab8d44f8e1ad885100ba614a8a589de5a)
216
+ Thu Nov 30 15:12:53 2023 -0500
217
+ - This commit includes two main changes. First, the error handling in 'app.py' was refactored to correct the namespace for 'openai.AuthenticationError'.
218
+ - Second, in 'llm_resources.py', the code was updated to include support for 'AzureOpenAIEmbeddings' in addition to the existing 'OpenAIEmbeddings'. This allows the application to use either OpenAI's standard embeddings or Azure's version, depending on the provided configuration.
219
+ ## [Upgraded project version from 0.3.0 to 1.0.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/a338494f3816bbb77360d2f43a515d1cc39cd760)
220
+ Wed Nov 29 16:05:50 2023 -0500
221
+ - Updated the current version in bumpver.toml from 0.3.0 to 1.0.0.
222
+ - Modified the Docker image version in kubernetes/resources.yaml from joshuasundance/langchain-streamlit-demo:0.3.0 to joshuasundance/langchain-streamlit-demo:1.0.0.
223
+ - Changed the __version__ variable in langchain-streamlit-demo/app.py from 0.3.0 to 1.0.0.
224
+ ## [Updated model list, library versions, and code formatting](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3ca1115e20d1709f4ede77b0ce63fafcf42a362c)
225
+ Wed Nov 29 16:01:38 2023 -0500
226
+ - 1. In the 'defaults.py' file, the list of models was updated. New models were added and existing model names were updated.
227
+ - 2. In the 'llm_resources.py' file, a minor code formatting change was made.
228
+ - 3. In the 'requirements.txt' file, the versions of several libraries were updated, including 'anthropic', 'langchain', 'langsmith', and 'openai'.
229
+ ## [Updated version from 0.2.0 to 0.3.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/2d1ba795654b4c65e705f42a92eae7a382cc8e66)
230
+ Mon Nov 27 16:46:11 2023 -0500
231
+ - The version number has been updated from 0.2.0 to 0.3.0 in multiple files. This includes the bumpver.toml, which controls the project versioning, the Kubernetes resources.yaml, which specifies the Docker image version to use, and the main app.py file, which includes a version number for display in the web app.
232
+ ## [Updated caching decorator and minor code formatting](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/5cdfa32bdb13c91564a4e175bafe1bc04414d1d8)
233
+ Mon Nov 27 14:47:58 2023 -0600
234
+ - Replaced the `@st.cache_data` decorator with `@st.cache_resource` in `app.py` for the `get_texts_and_retriever_cacheable_wrapper` function. This change might be due to an update in the library or to utilize a more suitable caching method.
235
+ - In `llm_resources.py`, removed an unnecessary line break and added a space for better code readability.
236
+ ## [Updated OpenAI library version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/a88ba2b3040ffff0c93d1c6159b6120091f76e37)
237
+ Mon Nov 27 12:36:16 2023 +0000
238
+ - The OpenAI library version in the project's requirements has been updated from 0.28.1 to 1.3.5. This update may include new features, bug fixes, or performance improvements from the library.
239
+ ## [Updated langsmith package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/b3a6aa254b2acd40593ef0b0167e061cd5beb448)
240
+ Mon Nov 27 12:36:12 2023 +0000
241
+ - The langsmith package version in requirements.txt was updated from 0.0.63 to 0.0.66.
242
+ ## [Updated langchain version in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d6340145557e4727e4521c530efa8abe16060882)
243
+ Mon Nov 27 12:36:07 2023 +0000
244
+ - The langchain package version has been updated from 0.0.334 to 0.0.340 in the requirements.txt file. This update may include new features, bug fixes, or performance improvements.
245
+ ## [Updated anthropic package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/264cec5ebc6b618397d3b9449bf5277c3ad53c96)
246
+ Mon Nov 27 12:36:01 2023 +0000
247
+ - The anthropic package version in requirements.txt was updated from 0.5.0 to 0.7.4. No other dependencies were changed.
248
+ ## [Updated pypdf version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/325c8315d4b388c71776a939a5267776940b26ce)
249
+ Mon Nov 20 12:35:08 2023 +0000
250
+ - The pypdf library was updated from version 3.17.0 to 3.17.1 in the requirements.txt file.
251
+ ## [Updated project version to 0.2.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/97d4687085a8c28a8c0a3a082e9cfa70d2fe853e)
252
+ Fri Nov 10 21:30:40 2023 -0500
253
+ - The version number in bumpver.toml has been updated from 0.1.2 to 0.2.0.
254
+ - In the Kubernetes resources.yaml file, the image version of langchain-streamlit-demo has been updated to reflect the new version.
255
+ - The __version__ variable in the app.py file of the langchain-streamlit-demo project has also been updated to 0.2.0.
256
+ ## [Updated pillow library version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f61858c54e1a37f8a7a734ca2c84c66cd62d77e4)
257
+ Sat Nov 11 02:07:34 2023 +0000
258
+ - The pillow library version was updated from 10.0.0 to 10.0.1 in the requirements.txt file. This change was made to avoid a vulnerability.
259
+ ## [Updated package versions in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d4691b420e22e6b27a3d968f1295d97bb3191376)
260
+ Fri Nov 10 21:04:08 2023 -0500
261
+ - Updated the versions of the 'langchain', 'langsmith', and 'streamlit' packages in the requirements.txt file.
262
+ - Added 'pillow' and 'pyarrow' packages to the requirements.txt file.
263
+ - These changes ensure that the project is using the latest and most secure versions of the dependencies.
264
+ ## [Bumped application version from 0.1.1 to 0.1.2](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/a51a26dd0cf4b33726ac3aa84b41acc103b0c06f)
265
+ Wed Nov 1 16:03:52 2023 -0400
266
+ - Updated the application version in bumpver.toml, resources.yaml, and app.py. This includes the version used for the Docker image in the Kubernetes deployment configuration.
267
+ ## [Updated default checkbox value and removed initial chatbot message](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3d59c85771f67f633f9498ffa3705880576de914)
268
+ Wed Nov 1 15:51:00 2023 -0400
269
+ - Changed the default value of the 'Document Chat' checkbox to be true if a file is uploaded and false if not.
270
+ - Removed the condition that disables the 'Chain Type' dropdown when 'Document Chat' is not selected.
271
+ - Eliminated the automatic 'Hello! I'm a helpful AI chatbot. Ask me a question!' message when the chat history is empty.
272
+ ## [Version Bump to 0.1.1](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/fc0e83182e47a9f41465fa815b286455b10e78f9)
273
+ Wed Nov 1 13:58:11 2023 -0400
274
+ - This commit represents a version bump from 0.1.0 to 0.1.1. Changes were made in the bumpver.toml file to update the current version. The Docker image reference in the Kubernetes resources.yaml file was also updated to reflect the new version. Lastly, the __version__ variable in the langchain-streamlit-demo/app.py file was updated.
275
+ ## [Handled additional exception in app.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/8a23b378977a263201791101e5a0ebc56e4f5f05)
276
+ Wed Nov 1 13:55:35 2023 -0400
277
+ - Updated the exception handling in app.py to include LangSmithNotFoundError along with the existing LangSmithError. This change improves the robustness of the error handling mechanism.
278
+ ## [Updated project version to 0.1.0](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/bbb9f000d8907e12c2aea643fb01e234b8d771bc)
279
+ Mon Oct 30 12:03:02 2023 -0400
280
+ - The project's version number has been updated from 0.0.16 to 0.1.0 in the bumpver.toml file, kubernetes resource file, and the main application file.
281
+ ## [Added mistralai/Mistral-7B-Instruct-v0.1 to Anyscale Endpoints](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d8cef94cffde0307292685d7273a0bf7a0974d02)
282
+ Mon Oct 30 11:31:43 2023 -0400
283
+ - In the README.md file, a new endpoint mistralai/Mistral-7B-Instruct-v0.1 was added under the section of Anyscale Endpoints.
284
+ - In the defaults.py file, the same endpoint was added to the MODEL_DICT dictionary under the key-value pair 'mistralai/Mistral-7B-Instruct-v0.1': 'Anyscale Endpoints'.
285
+ - The SUPPORTED_MODELS list was updated accordingly to include this new endpoint.
286
+ ## [Updated langsmith package version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/018041a3bdd72aaf3ab62b6eecba51ac18c93bcd)
287
+ Mon Oct 30 12:50:15 2023 +0000
288
+ - The langsmith package version in requirements.txt has been updated from 0.0.49 to 0.0.53. This update might include bug fixes, new features, or improvements.
289
+ ## [Updated langchain version in requirements](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/1215664e1bcb9b0a1f7f90a608fa16dc68dbbd0a)
290
+ Mon Oct 30 12:49:54 2023 +0000
291
+ - The langchain package version in requirements.txt has been updated from 0.0.320 to 0.0.325. This update might include bug fixes, security patches or new features.
292
+ ## [Bump version from 0.0.15 to 0.0.16](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/04871e0362967e38aceb00aa4fd13818a793ff1a)
293
+ Mon Oct 23 12:57:22 2023 -0400
294
+ - Updated the current version in bumpver.toml from 0.0.15 to 0.0.16.
295
+ - In the Kubernetes resources.yaml, updated the image version for langchain-streamlit-demo from 0.0.15 to 0.0.16.
296
+ - In langchain-streamlit-demo/app.py, updated the __version__ variable from 0.0.15 to 0.0.16.
297
+ ## [Updated package versions in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/839541a4fc5515d4554a36946001f1cee80f6fdc)
298
+ Mon Oct 23 12:49:36 2023 -0400
299
+ - Updated the versions of 'anthropic', 'langchain', and 'langsmith' in the requirements file. 'anthropic' is updated from version 0.3.11 to 0.5.0, 'langchain' from 0.0.315 to 0.0.320, and 'langsmith' from 0.0.44 to 0.0.49.
300
+ ## [Added 'validators' package to requirements](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/a1e0ab15cde332cd8efcba310fb67e25cb990783)
301
+ Fri Oct 20 22:54:23 2023 +0000
302
+ - The 'validators' package was added to the requirements.txt file. This package is not directly required by the project, but it has been pinned by Snyk to version 0.21.0 or newer to avoid a potential vulnerability.
303
+ ## [Updated badges in README](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/32f02019c445fb88beb73f00c6ffd0a17ff2a5d3)
304
+ Thu Oct 19 15:19:10 2023 -0400
305
+ - Replaced the Docker badge with a 'Push to Docker Hub' GitHub Actions workflow badge.
306
+ - Added a 'Push to HuggingFace Space' GitHub Actions workflow badge.
307
+ - Added an 'Update AI Changelog on Push to Main' GitHub Actions workflow badge.
308
+ ## [Added Azure OpenAI Service to README](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3353ff5eaa74c050414bf6b67ac590ac25d19f74)
309
+ Thu Oct 19 11:08:36 2023 -0400
310
+ - Updated README.md to include Azure OpenAI Service in the list of services and endpoints. A placeholder for configurable endpoints under Azure OpenAI Service has also been added.
311
+ ## [Added Black component to misc.xml and updated badges in README.md](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/1fb27e8b839a4c3d526da009c05ef3c08a1c2786)
312
+ Thu Oct 19 10:49:24 2023 -0400
313
+ - The commit introduces two changes:
314
+ - 1. A new Black component is added to the .idea/misc.xml file. This suggests that the Black Python code formatter has been configured for the project.
315
+ - 2. The README.md file has been updated to include new badges for code maintainability, issues, technical debt, and known vulnerabilities. The order of the existing badges has also been rearranged.
316
+ ## [Version Bump from 0.0.14 to 0.0.15](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/cc115e36633b7d899076da029c59dda03ca177ec)
317
+ Mon Oct 16 14:09:34 2023 -0400
318
+ - The version number has been increased from 0.0.14 to 0.0.15. This change has been reflected in the bumpver.toml file, the Kubernetes resources file, and the langchain-streamlit-demo app.py file. The Docker image used in the Kubernetes resources file has also been updated to reflect this new version number.
319
+ ## [Updated several package versions in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/cfa1e0b55c4f108b30ff6c7389668f1677f91437)
320
+ Mon Oct 16 14:02:49 2023 -0400
321
+ - Updated the version of langchain from 0.0.308 to 0.0.315.
322
+ - Updated the version of langsmith from 0.0.43 to 0.0.44.
323
+ - Updated the version of pypdf from 3.16.2 to 3.16.4.
324
+ ## [Updated environment variable name in Kubernetes config](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/62281947edc36d93259d723b8b4b63f3b9b646d1)
325
+ Fri Oct 6 21:19:03 2023 -0400
326
+ - In the Kubernetes configuration file 'resources.yaml', the environment variable name 'SHOW_LANGCHAIN_OPTIONS' was replaced with 'SHOW_LANGSMITH_OPTIONS'. This change reflects an update in the naming convention or the service being used.
327
+ ## [Bumped application version to 0.0.14](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/eb059075d36e4b09269df5b75dbea1b0e4e22f11)
328
+ Fri Oct 6 20:59:11 2023 -0400
329
+ - Updated the version of the application in bumpver.toml, kubernetes/resources.yaml, and langchain-streamlit-demo/app.py from 0.0.13 to 0.0.14.
330
+ ## [Refactored app.py to use Streamlit session state for storing global variables](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/e9f7a777844336b99d3bc8c2270e77e2acb0e7e7)
331
+ Fri Oct 6 20:47:25 2023 -0400
332
+ - This commit refactors the app.py file of the langchain-streamlit-demo to use Streamlit's session state for storing global variables. This includes API keys, project names, and Azure configurations. A new function 'azure_state_or_default' has been introduced to update the session state for Azure configurations. This change allows for better state management and persistence across multiple sessions.
333
+ ## [Added input field for Azure OpenAI EMB deployment name](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/b44c9a31a33a9b7d3b0e347a0ffe4ea31c068e81)
334
+ Fri Oct 6 18:40:55 2023 -0400
335
+ - An input field for the Azure OpenAI EMB deployment name has been added to the sidebar of the Streamlit application. This allows users to specify the name of their Azure OpenAI EMB deployment.
336
+ ## [Added Azure OpenAI Embeddings option to app](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/c60388636d63567bd9bfe4b7bbfebf734d3100da)
337
+ Fri Oct 6 18:35:40 2023 -0400
338
+ - This commit introduces the option to use Azure OpenAI for embeddings in the langchain-streamlit-demo app. It adds the necessary environment variables and updates the code to handle the new option. The changes include:
339
+ - 1. Addition of the AZURE_OPENAI_EMB_DEPLOYMENT_NAME environment variable in the Kubernetes resources.
340
+ - 2. Update of the app.py file to handle the Azure OpenAI option. If Azure embeddings are available, a toggle is displayed to the user to switch between Azure and OpenAI directly.
341
+ - 3. Update of the get_texts_and_retriever function in llm_resources.py to accept additional arguments for azure_kwargs and use_azure.
342
+ - 4. Update of the defaults.py file to include the AZURE_OPENAI_EMB_DEPLOYMENT_NAME in the list of Azure environment variables.
343
+ ## [Refactored code to improve readability and maintainability](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/0ce4fb3a9cb43ee563729df1d6b682511e17248f)
344
+ Fri Oct 6 18:15:24 2023 -0400
345
+ - 1. Updated kubernetes resource configuration to add environment variables for SHOW_LANGCHAIN_OPTIONS and SHOW_AZURE_OPTIONS.
346
+ - 2. Refactored the app.py script to import default values from a single source, improving readability and maintainability of the code.
347
+ - 3. Updated defaults.py to define a namedtuple for default values, which is imported in other scripts.
348
+ - 4. Modified llm_resources.py to accommodate changes in the import of default values.
349
+ ## [Refactor code by moving logic to a separate module](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/21eccfc51cf90268826929cddbf2bfa42bc2f5eb)
350
+ Fri Oct 6 16:26:26 2023 -0400
351
+ - The commit moves a significant amount of logic from 'app.py' to a new module named 'llm_resources.py'. This includes the methods for getting the runnable instance, the language model, and the texts and retriever. The aim of this refactoring is to improve code organization, readability, and maintainability.
352
+ ## [Refactored code and improved project structure](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/87d698488900d63b992059b6f291d6981773fb4b)
353
+ Fri Oct 6 15:59:43 2023 -0400
354
+ - Moved model constants and environment variables into a separate 'defaults.py' file for better code organization and readability.
355
+ - Updated 'app.py' to import these constants and variables from the new 'defaults.py' file.
356
+ - Modified '.idea/langchain-streamlit-demo.iml' to include a new source folder, improving the project's structure.
357
+ ## [Added Azure OpenAI environment variables to Kubernetes deployment](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f39ac3b55d8e57db36ff4a43a4b95dda1fa46e9d)
358
+ Fri Oct 6 14:15:57 2023 -0400
359
+ - In the Kubernetes resource configuration file, several environment variables related to Azure OpenAI have been added. These include the base URL, API version, deployment name, API key, and model version. The values for these variables are fetched from the 'langchain-streamlit-demo-secret' secret.
360
+ ## [Bumped version from 0.0.12 to 0.0.13](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d767997980a751389dcbec81b1bcaa1c10267534)
361
+ Fri Oct 6 14:03:44 2023 -0400
362
+ - Updated the current_version in bumpver.toml from 0.0.12 to 0.0.13.
363
+ - Updated the image tag in the Kubernetes resources.yaml file to use the new version 0.0.13.
364
+ - Updated the __version__ variable in the app.py file to reflect the new version 0.0.13.
365
+ ## [Refactored application code and updated dependencies](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/979e3bd9fe449bea04e5ceda5c1a72be2e824c58)
366
+ Fri Oct 6 13:58:33 2023 -0400
367
+ - Refactored the application code in 'langchain-streamlit-demo/app.py' to improve clarity and organization. Changes include renaming 'AZURE' to 'Azure OpenAI' in the 'MODEL_DICT' and modifying related conditional checks, renaming 'Advanced Options' to 'Advanced Settings', and restructuring 'LangSmith Options' into its own section within the sidebar.
368
+ - Updated the 'streamlit' version from '1.27.1' to '1.27.2' in 'requirements.txt'.
369
+ ## [Added support for Azure Chat models in the Streamlit application](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/72c3d8c60b3e15ce8d89f926ffe2ab845d3d9c1b)
370
+ Fri Oct 6 13:50:43 2023 -0400
371
+ - The commit introduces Azure Chat models into the Streamlit application. It includes the addition of the AzureChatOpenAI model in the import statement and the MODEL_DICT. Environment variables for Azure are also defined and retrieved from the system environment. User interface elements for Azure options have been added within an expandable section in the sidebar. Finally, an instance of AzureChatOpenAI is created if all Azure details are available and the selected provider is Azure.
372
+ ## [Updated langsmith package](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/e4b72fedeb71c822b6a76ed84199fef2bbc3bf8a)
373
+ Fri Oct 6 13:02:43 2023 -0400
374
+ - The langsmith package version was updated from 0.0.41 to 0.0.43 in the requirements.txt file.
375
+ ## [Updated langchain version in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/2c41972749e5524bba738b37e6d31416e657fec6)
376
+ Thu Oct 5 13:54:14 2023 +0000
377
+ - The langchain package version in requirements.txt has been upgraded from 0.0.305 to 0.0.308. This update may include bug fixes, feature enhancements or performance improvements.
378
+ ## [Updated application version to 0.0.12](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/2bee8f19e2fa71c333588a3531b55fe062701328)
379
+ Mon Oct 2 09:13:48 2023 -0400
380
+ - The application version has been updated from 0.0.11 to 0.0.12 in three different files. These include bumpver.toml, resources.yaml under kubernetes, and app.py under langchain-streamlit-demo. In bumpver.toml, the current_version value is updated. In resources.yaml, the image version for the container 'langchain-streamlit-demo' is updated. In app.py, the __version__ variable is updated to reflect the new version.
381
+ ## [Updated dependencies in requirements.txt](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/9747a2d97d4e60861e6d0cc8de7ca8076a6ac971)
382
+ Mon Oct 2 09:10:05 2023 -0400
383
+ - The langchain and langsmith dependencies have been updated to versions 0.0.305 and 0.0.41 respectively.
384
+ - The openai dependency has been updated to version 0.28.1.
385
+ - The previous comment about rolling back the langchain update to avoid a bug has been removed, implying the bug has been fixed in the new version.
386
+ ## [Version Bump from 0.0.10 to 0.0.11](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/58978f749bdf319a2c2f76e74a46e7d905b7bf69)
387
+ Sat Sep 30 01:31:32 2023 -0400
388
+ - Updated the current_version in bumpver.toml from 0.0.10 to 0.0.11.
389
+ - In the Kubernetes resources.yaml, updated the image version of langchain-streamlit-demo from 0.0.10 to 0.0.11.
390
+ - In the langchain-streamlit-demo/app.py, updated the __version__ from 0.0.10 to 0.0.11.
391
+ ## [Updated README.md with minor content and structure changes](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/241c14d23e150e0be6edee2c28e32c1b4a519c73)
392
+ Sat Sep 30 01:29:08 2023 -0400
393
+ - This commit includes changes to the README.md file. The authorship of the README has been clarified to indicate that it was originally written by Claude 2. The Features section has been updated to include a new model from Anyscale Endpoints, and to mention the addition of various forms of document chat. The Code Overview section was removed. A minor formatting change was made to the Docker run command. The Docker Compose instructions were simplified by removing a redundant command.
394
+ ## [Improved UI labels and refactored code in langchain-streamlit-demo](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f8e912146cbca42cbd456abb123b5223b0924c45)
395
+ Sat Sep 30 01:24:47 2023 -0400
396
+ - This commit includes changes to improve the user interface labels for better readability. The labels 'chunk_size' and 'chunk_overlap' have been changed to 'Number of Tokens per Chunk' and 'Chunk Overlap' respectively.
397
+ - Additionally, the code for handling the full response and the initialization of the `st.session_state.chain` has been refactored for better readability and maintainability. The code now clearly distinguishes between the cases when `use_document_chat` is true or false, and the initialization of `st.session_state.chain` is more streamlined.
398
+ ## [Refactored chat functionality and removed unnecessary code in app.py and qagen.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/923e6fac55336c07c8af10b74742b117517bd757)
399
+ Sat Sep 30 01:10:43 2023 -0400
400
+ - In app.py, removed the StreamlitCallbackHandler import and simplified the logic for handling chat inputs. Removed the document chat condition in the if statement, and directly implemented the regular chat functionality. Simplified the condition for using document chat, and refactored the way rag_runnable is retrieved based on the document chat chain type.
401
+ - In qagen.py, removed the unnecessary import of reduce from functools and the combine_qa_pair_lists function. Simplified the get_rag_qa_gen_chain function by directly converting the parsed_output to a string.
402
+ ## [Refactored code for readability and efficiency](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/bfaa0c3cf1792d9cb5086657f9ded983ba616662)
403
+ Fri Sep 29 23:12:56 2023 -0400
404
+ - This commit includes changes in the 'app.py' and 'qagen.py' files. In 'app.py', the code has been refactored to improve readability and efficiency. The configuration dictionary has been moved outside the if-conditions to avoid redundancy. Also, the condition checking for 'Summarization' and 'Q&A Generation' has been combined to reduce nested if-statements.
405
+ - In the 'qagen.py' file, two new methods 'to_str' have been added to the 'QuestionAnswerPair' and 'QuestionAnswerPairList' classes. These methods convert the question and answer pairs into a string format. This change has moved the responsibility of string formatting from 'app.py' to 'qagen.py', making the code more modular and easier to maintain.
406
+ ## [Updated summarization functionality in Streamlit app](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/dd9bfbddff559ce0065c236ccc6419f987a61664)
407
+ Fri Sep 29 22:43:24 2023 -0400
408
+ - Replaced the existing get_summarization_chain function with get_rag_summarization_chain in the Streamlit app.
409
+ - The get_rag_summarization_chain function now takes in the prompt, retriever and the language model as parameters.
410
+ - Refactored the way the summarization chain is invoked and the full response is generated.
411
+ - Updated the get_rag_summarization_chain function in the summarize module to return a RunnableSequence.
412
+ ## [Updated model_name parameter in ChatOpenAI instantiation](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/06099804b5d0a4d635beb8a0021ac84b22cb0529)
413
+ Fri Sep 29 18:38:23 2023 -0400
414
+ - Replaced hardcoded 'test' value for model_name parameter with a variable named 'model'. This change allows the model name to be dynamically set when the ChatOpenAI class is instantiated.
415
+ ## [Refactored model instantiation and removed deprecated functions](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/c2ef57040f3231cc3fa80157d93d0d8420f21351)
416
+ Fri Sep 29 18:38:01 2023 -0400
417
+ - Updated the instantiation of ChatOpenAI, ChatAnthropic, and ChatAnyscale classes by swapping the model and model_name parameters to match their class definitions.
418
+ - Removed the commented out get_qa_gen_chain function in qagen.py.
419
+ - Removed commented out code related to raw_results and results in app.py, simplifying the logic.
420
+ ## [Refactored the data processing pipeline](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/8106321374be538f2740587a9a3d68e9cb82310f)
421
+ Fri Sep 29 18:31:46 2023 -0400
422
+ - Removed the 'combine_qa_pair_lists' function from the data processing pipeline in 'app.py'.
423
+ - Directly accessed 'QuestionAnswerPairs' from 'raw_results' instead of using 'combine_qa_pair_lists' function.
424
+ - Commented out the print statement for 'raw_results'.
425
+ ## [Refactored code to change 'input' to 'context' in langchain-streamlit-demo](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3550ebd119e342869167a228929538c069350942)
426
+ Fri Sep 29 18:19:58 2023 -0400
427
+ - This commit includes a change in the variable name from 'input' to 'context' in both app.py and qagen.py files. The change was made in the section where the document page content is being processed. This change is likely aimed at improving code readability and consistency.
428
+ ## [Added customizability for number of chunks in retriever](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/457889e5dc68143a1a5f935ca6af849bd380666c)
429
+ Fri Sep 29 18:16:51 2023 -0400
430
+ - This commit introduces a slider in the UI allowing the user to select the number of chunks that will be used for context in the retriever. The 'get_texts_and_retriever' function was updated to include a new parameter 'k' that defaults to the newly introduced constant 'DEFAULT_RETRIEVER_K'. This 'k' value is then used in the creation of both the 'bm25_retriever' and 'faiss_retriever'.
431
+ ## [Updated Q&A Generation method and invocation](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/8aab446c1cbafbdb04dcf6d56ed77413f1e63f65)
432
+ Fri Sep 29 18:13:30 2023 -0400
433
+ - Replaced the get_qa_gen_chain method with get_rag_qa_gen_chain in app.py and qagen.py. This change updates the Q&A Generation method used in the Document Chat feature.
434
+ - Changed the way the Q&A Generation method is invoked. Instead of using the batch method, we now use the invoke method. This change is expected to improve the efficiency of the Document Chat feature.
435
+ ## [Implemented RAG-based Q&A Generation Chain](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/6467ea59cd8b5eb4859d20c8f84152402833cb92)
436
+ Fri Sep 29 18:04:08 2023 -0400
437
+ - Added a new function 'get_rag_qa_gen_chain' in 'qagen.py' to set up a RAG-based Q&A generation chain using a retriever and a language model.
438
+ - Adjusted the 'app.py' to include a commented-out option to use the new RAG-based Q&A generation chain.
439
+ ## [Bump version from 0.0.9 to 0.0.10](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/bb29f017b57cd891d1f9ae86e212ec6c92b5aa43)
440
+ Fri Sep 29 13:17:34 2023 -0400
441
+ - Updated the version number in the bumpver.toml, kubernetes/resources.yaml, and langchain-streamlit-demo/app.py files. The new version is 0.0.10.
442
+ ## [Updated retriever logic and removed question validation](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/930d4126fe97dbb403fa498b5322e10815d06179)
443
+ Fri Sep 29 11:17:45 2023 -0400
444
+ - In the 'langchain-streamlit-demo/app.py' file, the logic to retrieve texts has been updated. The FAISS retriever has been replaced with an ensemble retriever that uses both the BM25 and FAISS retrievers. The BM25 retriever's 'k' parameter has been set to 4, and the FAISS retriever has been updated to use a vector store.
445
+ - In the 'langchain-streamlit-demo/qagen.py' file, the field validator for the 'question' field in the 'QuestionAnswerPair' class has been removed. This means that questions no longer need to end with a question mark to be valid.
446
+ - The 'requirements.txt' file has been updated to include the 'rank_bm25==0.2.2' package, and the 'streamlit' package has been updated to version '1.27.1'.
447
+ ## [Updated the refine_template in summarize.py](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/736288ed897a6bf1b5c0be7c0481011598351395)
448
+ Thu Sep 28 20:55:25 2023 -0400
449
+ - The refine_template string in the summarize.py file has been updated. A newline character has been added after the 'User input: {query}' part of the string for better readability.
450
+ ## [Updated application version to 0.0.9](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/40604bea7723c4c05b4c36289950e6d8a25b7690)
451
+ Thu Sep 28 20:41:33 2023 -0400
452
+ - The version number in the bumpver.toml file has been updated from 0.0.8 to 0.0.9.
453
+ - The Docker image version for the langchain-streamlit-demo app in the Kubernetes resources.yaml file has been updated from 0.0.8 to 0.0.9.
454
+ - The __version__ variable in the app.py file of the langchain-streamlit-demo app has been updated from 0.0.8 to 0.0.9.
455
+ ## [Improved text formatting in Q&A response](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/4eaf9de17247ed7e6bdc1771ff31639cca9e903d)
456
+ Thu Sep 28 20:39:28 2023 -0400
457
+ - This commit adjusts the formatting of the Q&A response in the langchain-streamlit-demo app. It adds an extra newline between the question and answer parts, and another newline between each Q&A pair for better readability.
458
+ ## [Updated application version to 0.0.8](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/8c80fe821129d05b8b714beb56a4e0bbca6ce676)
459
+ Thu Sep 28 20:36:46 2023 -0400
460
+ - The application's version number has been updated from 0.0.7 to 0.0.8 in the following files: bumpver.toml, resources.yaml, and app.py.
461
+ - In bumpver.toml, the current_version field was updated to reflect the new version.
462
+ - In resources.yaml, the image tag for the langchain-streamlit-demo container was updated to use the new version.
463
+ - In app.py, the __version__ variable was updated to the new version.
464
+ ## [Refactor variable names in Streamlit app](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/9bf9004ce3ba4160e1c33f57e0c5e48c0ff4f628)
465
+ Thu Sep 28 20:33:17 2023 -0400
466
+ - The variable 'output_text' was renamed to 'full_response' in the Streamlit application to better reflect its purpose. This change improves code readability and understanding.
467
+ ## [Bumped version from 0.0.6 to 0.0.7](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/d41f4a4356709af4dbd81982fdefb0a6dba21ef6)
468
+ Thu Sep 28 19:56:12 2023 -0400
469
+ - Updated the version number in bumpver.toml, resources.yaml, and app.py.
470
+ - This commit includes changes to the version number in the bumpver configuration, the Docker image tag in the Kubernetes resources, and the version variable in the app.py file.
471
+ ## [Added Summarization Feature to Streamlit App](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/47c2ffc283d1e1754c1f64ab5fb793694bc9f24f)
472
+ Thu Sep 28 19:53:59 2023 -0400
473
+ - This commit introduces a summarization feature to the Streamlit application. It does so by creating a new 'summarize.py' file and integrating it into the 'app.py' file.
474
+ - In 'app.py', the 'LLMChain' import has been moved and the 'get_summarization_chain' function has been imported from 'summarize.py'.
475
+ - A new option 'Summarization' has been added to the 'Document Chat Chain Type' dropdown menu.
476
+ - When 'Summarization' is selected from the dropdown, the 'get_summarization_chain' function is called to create a summarization chain.
477
+ - The summarization chain is then used to generate a summary of the document, which is displayed in the Streamlit app.
478
+ - In the 'summarize.py' file, a new summarization chain is defined using the 'load_summarize_chain' function from the 'langchain.chains.summarize' module. The chain uses two custom prompt templates for summarizing and refining the document text.
479
+ ## [Enhanced document chat functionality in langchain-streamlit-demo](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/622ac6610de2f89368031d57ebd148259e5d7fcc)
480
+ Thu Sep 28 16:55:16 2023 -0400
481
+ - This commit includes enhancements to the document chat functionality in the langchain-streamlit-demo application. It introduces a new document chat chain type 'Q&A Generation' and updates the provider variable to be stored in the session state. The commit also adds a new file 'qagen.py' which contains code for generating question and answer pairs from a given text.
482
+ ## [Bump version from 0.0.5 to 0.0.6](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/f431ca56717b9e704226c3448a552fe31c90d77d)
483
+ Thu Sep 28 14:42:31 2023 -0400
484
+ - The version number in the 'bumpver.toml', 'kubernetes/resources.yaml', and 'langchain-streamlit-demo/app.py' files has been updated from 0.0.5 to 0.0.6. This indicates a new iteration of the software with potential minor changes or bug fixes.
485
+ ## [Updated ruff-pre-commit version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/50b28c9ac810cf9ff1c58e0b98f4ca7dfe3f94f5)
486
+ Wed Sep 27 20:58:22 2023 -0400
487
+ - The ruff-pre-commit version in the pre-commit configuration file was updated from v0.0.290 to v0.0.291.
488
+ ## [Updated file exclusions in pre-commit config](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/c8b46036933d50ca6befc5d4fa43bcb29f05c75a)
489
+ Wed Sep 27 20:57:54 2023 -0400
490
+ - The pre-commit configuration has been updated to exclude the AI_CHANGELOG.md file. Previously, the configuration was set to exclude .idea and docs directories. The repository and hook details remain unchanged.
491
+ ## [Refactored chain_type_help in app.py and updated AI_CHANGELOG.md](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/a1b0a6fd0b22021079e741929eb7671855192cb0)
492
+ Wed Sep 27 20:56:47 2023 -0400
493
+ - In app.py, the chain_type_help dictionary was refactored to directly generate a string with the help links for each chain_type_name, removing the need for a separate dictionary.
494
+ - In AI_CHANGELOG.md, a newline was added at the end of the file and entries were made for the addition of numpy and tornado to requirements.txt and the update of the token used for code checkout in the GitHub workflow.
495
+ ## [Updated GitHub Action to Push to HuggingFace Space](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/e95e574c846541fd959bd0d0355178fae542dd8e)
496
+ Wed Sep 27 17:05:25 2023 +0000
497
+ - This commit modifies the triggering conditions for the GitHub Action workflow that pushes updates to HuggingFace Space. Previously, the workflow was triggered on each push with any tag. Now, it is triggered upon completion of the 'Update AI Changelog on Push to Main' workflow on the 'main' branch.
498
+ - Additionally, the 'push-to-huggingface' job has been updated to depend on the completion of the 'update-changelog' job.
499
+ ## [Updated version number from 0.0.2 to 0.0.5](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/55fa7419137cf54127cbd03114c0c0284397cfd9)
500
+ Wed Sep 27 10:56:40 2023 -0400
501
+ - The version number in the bumpver configuration file has been updated from 0.0.2 to 0.0.5.
502
+ - The image version in the Kubernetes resources file has been updated to match the new version number.
503
+ - The __version__ variable in the langchain-streamlit-demo app has been updated to reflect the new version number.
504
+ ## [Updated page title to include version number](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/783c740fe52c44c3f3d9d5ad78b6c1784fa93e97)
505
+ Wed Sep 27 10:46:57 2023 -0400
506
+ - The page title of the Streamlit application was previously just the name of the application. The change now includes the version number in the title, which will make it easier to track and verify the version of the application in use.
507
+ ## [Added 'codellama/CodeLlama-34b-Instruct-hf' to Model Dictionary](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/68f6d34a4cefd91425cbc215f323fbd57dd6e4a7)
508
+ Wed Sep 27 10:46:24 2023 -0400
509
+ - The commit introduces a new model 'codellama/CodeLlama-34b-Instruct-hf' into the MODEL_DICT dictionary. This update extends the list of models supported by the 'Anyscale Endpoints'.
510
+ ## [Bumped version from 0.0.1 to 0.0.2](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/9feadf73e8c66425a565c99ce3088249bc4699f1)
511
+ Wed Sep 27 00:03:38 2023 -0400
512
+ - Updated the version number in bumpver.toml, Kubernetes resources.yaml, and the app.py file of the langchain-streamlit-demo application. The new version is 0.0.2.
513
+ ## [Updated app version](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/a1065bb282837cf191d30bcb45c638bd15c5b77a)
514
+ Wed Sep 27 00:00:28 2023 -0400
515
+ - The version number in langchain-streamlit-demo/app.py was updated from 0.0.0 to 0.0.1.
516
+ ## [Updated image version in Kubernetes resources and bumpver file](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/35cffe74d37db50ad5ae17a6e6af4d2131c1a5c3)
517
+ Tue Sep 26 23:59:47 2023 -0400
518
+ - In the 'bumpver.toml' file, the image version placeholder in 'kubernetes/resources.yaml' was corrected by removing the unnecessary quotes.
519
+ - In the 'kubernetes/resources.yaml' file, the image version for 'langchain-streamlit-demo' was updated from 'latest' to '0.0.1'.
520
+ ## [Implement versioning and modify GitHub workflows](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/7f34c9b5e16996dcb8eb5cdd3f5cdc86d7bf2b11)
521
+ Tue Sep 26 23:58:24 2023 -0400
522
+ - Introduced semantic versioning using bumpver. The current version is now tracked in a new file 'bumpver.toml' and also reflected in 'app.py' and the Docker image tag in 'kubernetes/resources.yaml'.
523
+ - Modified GitHub workflows 'docker-hub.yml' and 'hf-space.yml' to trigger on new tags instead of pushes to the main branch. The Docker image tag is now the release version instead of the git SHA.
524
+ - Removed the step to store the git SHA in 'docker-hub.yml'.
525
+ - No functional changes were made to 'langchain-streamlit-demo/app.py' or 'kubernetes/resources.yaml'. The imagePullPolicy remains as 'Always'.
526
+ ## [Updated requirements.txt for better package management](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/5085ade2d646a2670929e518e78b881ea2ffd0a5)
527
+ Tue Sep 26 23:14:05 2023 -0400
528
+ - Rolled back langchain package from version 0.0.301 to 0.0.300 to avoid a bug in langchain's chatanthropic.
529
+ - Pinned numpy to version 1.22.2 as suggested by Snyk to avoid a vulnerability.
530
+ - Reordered the packages for better readability.
531
+ ## [Added numpy and tornado to requirements.txt to avoid vulnerabilities](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/3f0e220f9f77d561510dd04b09f1c3c509a5b28f)
532
+ Tue Sep 26 12:56:59 2023 +0000
533
+ - The numpy and tornado packages were added to the requirements.txt file. These packages are not directly required by our application but were added to avoid potential vulnerabilities as suggested by Snyk.
534
+ ## [Updated token used for code checkout in GitHub workflow](https://github.com/joshuasundance-swca/langchain-streamlit-demo/commit/b0c4e1ca12f86ea6113ee2c86d38c39d3035f395)
535
+ Tue Sep 26 08:56:55 2023 -0400
536
+ - In the GitHub Actions workflow file 'ai_changelog.yml', the personal access token used for checking out code has been updated. The token has been changed from 'PAT' to 'WORKFLOW_GIT_ACCESS_TOKEN'.
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim-bookworm
2
+
3
+ RUN adduser --uid 1000 --disabled-password --gecos '' appuser
4
+ USER 1000
5
+
6
+ ENV PYTHONDONTWRITEBYTECODE=1 \
7
+ PYTHONUNBUFFERED=1 \
8
+ PATH="/home/appuser/.local/bin:$PATH"
9
+
10
+ RUN pip install --user --no-cache-dir --upgrade pip
11
+ COPY ./requirements.txt /home/appuser/requirements.txt
12
+ RUN pip install --user --no-cache-dir --upgrade -r /home/appuser/requirements.txt
13
+
14
+ COPY ./langchain-streamlit-demo/ /home/appuser/langchain-streamlit-demo/
15
+
16
+ WORKDIR /home/appuser/langchain-streamlit-demo
17
+ EXPOSE 7860
18
+
19
+ CMD ["streamlit", "run", "/home/appuser/langchain-streamlit-demo/app.py", "--server.port", "7860", "--server.address", "0.0.0.0"]
LICENSE ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Joshua Sundance Bailey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
README.md ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: langchain-streamlit-demo
3
+ emoji: 🦜
4
+ colorFrom: green
5
+ colorTo: red
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: true
9
+ tags: [langchain, streamlit, docker]
10
+ ---
11
+
12
+ # langchain-streamlit-demo
13
+
14
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
15
+ [![python](https://img.shields.io/badge/Python-3.11-3776AB.svg?style=flat&logo=python&logoColor=white)](https://www.python.org)
16
+
17
+ [![Push to Docker Hub](https://github.com/joshuasundance-swca/langchain-streamlit-demo/actions/workflows/docker-hub.yml/badge.svg)](https://github.com/joshuasundance-swca/langchain-streamlit-demo/actions/workflows/docker-hub.yml)
18
+ [![Docker Image Size (tag)](https://img.shields.io/docker/image-size/joshuasundance/langchain-streamlit-demo/latest)](https://hub.docker.com/r/joshuasundance/langchain-streamlit-demo)
19
+
20
+ [![Push to HuggingFace Space](https://github.com/joshuasundance-swca/langchain-streamlit-demo/actions/workflows/hf-space.yml/badge.svg)](https://github.com/joshuasundance-swca/langchain-streamlit-demo/actions/workflows/hf-space.yml)
21
+ [![Open HuggingFace Space](https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-sm.svg)](https://huggingface.co/spaces/joshuasundance/langchain-streamlit-demo)
22
+
23
+ ![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/joshuasundance-swca/langchain-streamlit-demo)
24
+ ![Code Climate issues](https://img.shields.io/codeclimate/issues/joshuasundance-swca/langchain-streamlit-demo)
25
+ ![Code Climate technical debt](https://img.shields.io/codeclimate/tech-debt/joshuasundance-swca/langchain-streamlit-demo)
26
+
27
+ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
28
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)
29
+ [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
30
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
31
+
32
+ [![security: bandit](https://img.shields.io/badge/security-bandit-yellow.svg)](https://github.com/PyCQA/bandit)
33
+ ![Known Vulnerabilities](https://snyk.io/test/github/joshuasundance-swca/langchain-streamlit-demo/badge.svg)
34
+
35
+ [![Update AI Changelog on Push to Main](https://github.com/joshuasundance-swca/langchain-streamlit-demo/actions/workflows/ai_changelog.yml/badge.svg)](https://github.com/joshuasundance-swca/langchain-streamlit-demo/actions/workflows/ai_changelog.yml)
36
+
37
+
38
+ This project shows how to build a simple chatbot UI with [Streamlit](https://streamlit.io) and [LangChain](https://langchain.com).
39
+
40
+ This `README` was originally written by [Claude 2](https://www.anthropic.com/index/claude-2), an LLM from [Anthropic](https://www.anthropic.com/).
41
+
42
+ # Features
43
+ - Chat interface for talking to AI assistant
44
+ - Supports models from
45
+ - [OpenAI](https://openai.com/)
46
+ - `gpt-3.5-turbo`
47
+ - `gpt-4`
48
+ - [Anthropic](https://www.anthropic.com/)
49
+ - `claude-instant-v1`
50
+ - `claude-2`
51
+ - [Anyscale Endpoints](https://endpoints.anyscale.com/)
52
+ - `meta-llama/Llama-2-7b-chat-hf`
53
+ - `meta-llama/Llama-2-13b-chat-hf`
54
+ - `meta-llama/Llama-2-70b-chat-hf`
55
+ - `codellama/CodeLlama-34b-Instruct-hf`
56
+ - `mistralai/Mistral-7B-Instruct-v0.1`
57
+ - [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/ai-services/openai-service/)
58
+ - `[configurable]`
59
+ - Streaming output of assistant responses
60
+ - Leverages LangChain for dialogue and memory management
61
+ - Integrates with [LangSmith](https://smith.langchain.com) for tracing conversations
62
+ - Allows giving feedback on assistant's responses
63
+ - Tries reading API keys and default values from environment variables
64
+ - Parameters in sidebar can be customized
65
+ - Includes various forms of document chat
66
+ - Question/Answer Pair Generation
67
+ - Summarization
68
+ - Standard retrieval chains
69
+
70
+ # Deployment
71
+ `langchain-streamlit-demo` is deployed as a [Docker image](https://hub.docker.com/r/joshuasundance/langchain-streamlit-demo) based on the [`python:3.11-slim-bookworm`](https://github.com/docker-library/python/blob/81b6e5f0643965618d633cd6b811bf0879dee360/3.11/slim-bookworm/Dockerfile) image.
72
+ CI/CD workflows in `.github/workflows` handle building and publishing the image as well as pushing it to Hugging Face.
73
+
74
+ ## Run on HuggingFace Spaces
75
+ [![Open HuggingFace Space](https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-sm.svg)](https://huggingface.co/spaces/joshuasundance/langchain-streamlit-demo)
76
+
77
+ ## With Docker (pull from Docker Hub)
78
+
79
+ 1. _Optional_: Create a `.env` file based on `.env-example`
80
+ 2. Run in terminal:
81
+
82
+ `docker run -p 7860:7860 joshuasundance/langchain-streamlit-demo:latest`
83
+
84
+ or
85
+
86
+ `docker run -p 7860:7860 --env-file .env joshuasundance/langchain-streamlit-demo:latest`
87
+
88
+ 3. Open http://localhost:7860 in your browser
89
+
90
+ ## Docker Compose (build locally)
91
+ 1. Clone the repo. Navigate to cloned repo directory
92
+ 2. _Optional_: Create a `.env` file based on `.env-example`
93
+ 3. Run in terminal:
94
+
95
+ `docker compose up`
96
+
97
+ 4. Open http://localhost:7860 in your browser
98
+
99
+ ## Kubernetes
100
+ 1. Clone the repo. Navigate to cloned repo directory
101
+ 2. Create a `.env` file based on `.env-example`
102
+ 3. Run bash script: `/bin/bash ./kubernetes/deploy.sh`
103
+ 4. Get the IP address for your new service: `kubectl get service langchain-streamlit-demo`
104
+
105
+ # Links
106
+ - [Streamlit](https://streamlit.io)
107
+ - [LangChain](https://langchain.com)
108
+ - [LangSmith](https://smith.langchain.com)
109
+ - [OpenAI](https://openai.com/)
110
+ - [Anthropic](https://www.anthropic.com/)
111
+ - [Anyscale Endpoints](https://endpoints.anyscale.com/)
112
+ - [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/ai-services/openai-service/)
bumpver.toml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [bumpver]
2
+ current_version = "2.1.4"
3
+ version_pattern = "MAJOR.MINOR.PATCH"
4
+ commit_message = "bump version {old_version} -> {new_version}"
5
+ tag_message = "{new_version}"
6
+ tag_scope = "default"
7
+ pre_commit_hook = ""
8
+ post_commit_hook = ""
9
+ commit = true
10
+ tag = true
11
+ push = true
12
+
13
+ [bumpver.file_patterns]
14
+ "bumpver.toml" = [
15
+ 'current_version = "{version}"',
16
+ ]
17
+ "langchain-streamlit-demo/app.py" = ['__version__ = "{version}"']
18
+ "kubernetes/resources.yaml" = [' image: joshuasundance/langchain-streamlit-demo:{version}']
docker-compose.yml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ langchain-streamlit-demo:
5
+ image: langchain-streamlit-demo:latest
6
+ build: .
7
+ env_file:
8
+ - .env
9
+ ports:
10
+ - "${APP_PORT:-7860}:${APP_PORT:-7860}"
11
+ command: [
12
+ "streamlit", "run",
13
+ "/home/appuser/langchain-streamlit-demo/app.py",
14
+ "--server.port", "${APP_PORT:-7860}",
15
+ "--server.address", "0.0.0.0"
16
+ ]
kubernetes/deploy.sh ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ # Create a secret for environment variables
6
+ secretExists=$(kubectl get secret langchain-streamlit-demo-secret --ignore-not-found)
7
+
8
+ if [ -n "$secretExists" ]; then
9
+ echo "Secret 'langchain-streamlit-demo-secret' already exists. Deleting and recreating."
10
+ kubectl delete secret langchain-streamlit-demo-secret
11
+ else
12
+ echo "Secret 'langchain-streamlit-demo-secret' does not exist. Creating."
13
+ fi
14
+
15
+ kubectl create secret generic langchain-streamlit-demo-secret --from-env-file=.env
16
+
17
+
18
+ # Deploy to Kubernetes
19
+ kubectl apply -f kubernetes/resources.yaml
kubernetes/resources.yaml ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ apiVersion: apps/v1
2
+ kind: Deployment
3
+ metadata:
4
+ name: langchain-streamlit-demo-deployment
5
+ spec:
6
+ replicas: 1
7
+ selector:
8
+ matchLabels:
9
+ app: langchain-streamlit-demo
10
+ template:
11
+ metadata:
12
+ labels:
13
+ app: langchain-streamlit-demo
14
+ spec:
15
+ containers:
16
+ - name: langchain-streamlit-demo
17
+ image: joshuasundance/langchain-streamlit-demo:2.1.4
18
+ imagePullPolicy: Always
19
+ resources:
20
+ requests:
21
+ cpu: "100m"
22
+ memory: "200Mi"
23
+ limits:
24
+ cpu: "500m"
25
+ memory: "500Mi"
26
+ env:
27
+ - name: AZURE_OPENAI_BASE_URL
28
+ valueFrom:
29
+ secretKeyRef:
30
+ name: langchain-streamlit-demo-secret
31
+ key: AZURE_OPENAI_BASE_URL
32
+ - name: AZURE_OPENAI_API_VERSION
33
+ valueFrom:
34
+ secretKeyRef:
35
+ name: langchain-streamlit-demo-secret
36
+ key: AZURE_OPENAI_API_VERSION
37
+ - name: AZURE_OPENAI_DEPLOYMENT_NAME
38
+ valueFrom:
39
+ secretKeyRef:
40
+ name: langchain-streamlit-demo-secret
41
+ key: AZURE_OPENAI_DEPLOYMENT_NAME
42
+ - name: AZURE_OPENAI_EMB_DEPLOYMENT_NAME
43
+ valueFrom:
44
+ secretKeyRef:
45
+ name: langchain-streamlit-demo-secret
46
+ key: AZURE_OPENAI_EMB_DEPLOYMENT_NAME
47
+ - name: AZURE_OPENAI_API_KEY
48
+ valueFrom:
49
+ secretKeyRef:
50
+ name: langchain-streamlit-demo-secret
51
+ key: AZURE_OPENAI_API_KEY
52
+ - name: AZURE_OPENAI_MODEL_VERSION
53
+ valueFrom:
54
+ secretKeyRef:
55
+ name: langchain-streamlit-demo-secret
56
+ key: AZURE_OPENAI_MODEL_VERSION
57
+ - name: OPENAI_API_KEY
58
+ valueFrom:
59
+ secretKeyRef:
60
+ name: langchain-streamlit-demo-secret
61
+ key: OPENAI_API_KEY
62
+ - name: ANTHROPIC_API_KEY
63
+ valueFrom:
64
+ secretKeyRef:
65
+ name: langchain-streamlit-demo-secret
66
+ key: ANTHROPIC_API_KEY
67
+ - name: ANYSCALE_API_KEY
68
+ valueFrom:
69
+ secretKeyRef:
70
+ name: langchain-streamlit-demo-secret
71
+ key: ANYSCALE_API_KEY
72
+ - name: LANGCHAIN_API_KEY
73
+ valueFrom:
74
+ secretKeyRef:
75
+ name: langchain-streamlit-demo-secret
76
+ key: LANGCHAIN_API_KEY
77
+ - name: LANGCHAIN_PROJECT
78
+ value: "langchain-streamlit-demo"
79
+ - name: SHOW_LANGSMITH_OPTIONS
80
+ value: "False"
81
+ - name: SHOW_AZURE_OPTIONS
82
+ value: "False"
83
+ securityContext:
84
+ runAsNonRoot: true
85
+ ---
86
+ apiVersion: v1
87
+ kind: Service
88
+ metadata:
89
+ name: langchain-streamlit-demo-service
90
+ # configure on Azure and uncomment below to use a vnet
91
+ # annotations:
92
+ # service.beta.kubernetes.io/azure-load-balancer-internal: "true"
93
+ # service.beta.kubernetes.io/azure-load-balancer-ipv4: vnet.ip.goes.here
94
+ # service.beta.kubernetes.io/azure-dns-label-name: "langchain-streamlit-demo"
95
+ spec:
96
+ selector:
97
+ app: langchain-streamlit-demo
98
+ ports:
99
+ - protocol: TCP
100
+ port: 80
101
+ targetPort: 7860
102
+ type: LoadBalancer
103
+ ---
104
+ apiVersion: networking.k8s.io/v1
105
+ kind: NetworkPolicy
106
+ metadata:
107
+ name: langchain-streamlit-demo-network-policy
108
+ spec:
109
+ podSelector:
110
+ matchLabels:
111
+ app: langchain-streamlit-demo
112
+ policyTypes:
113
+ - Ingress
114
+ ingress:
115
+ - from: [] # An empty array here means it will allow traffic from all sources.
116
+ ports:
117
+ - protocol: TCP
118
+ port: 7860
langchain-streamlit-demo/.streamlit/config.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor="#F63366"
3
+ backgroundColor="#FFFFFF"
4
+ secondaryBackgroundColor="#F0F2F6"
5
+ textColor="#262730"
6
+ font="sans serif"
langchain-streamlit-demo/app.py ADDED
@@ -0,0 +1,627 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ from typing import Tuple, List, Dict, Any, Union, Optional
3
+
4
+ import anthropic
5
+ import langsmith.utils
6
+ import openai
7
+ import streamlit as st
8
+ from langchain.agents import load_tools
9
+ from langchain.agents.tools import tool
10
+ from langchain.callbacks import StreamlitCallbackHandler
11
+ from langchain.callbacks.base import BaseCallbackHandler
12
+ from langchain.callbacks.manager import Callbacks
13
+ from langchain.callbacks.tracers.langchain import LangChainTracer, wait_for_all_tracers
14
+ from langchain.callbacks.tracers.run_collector import RunCollectorCallbackHandler
15
+ from langchain.memory import ConversationBufferMemory, StreamlitChatMessageHistory
16
+ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
17
+ from langchain.schema.document import Document
18
+ from langchain.schema.retriever import BaseRetriever
19
+ from langchain.tools import DuckDuckGoSearchRun, WikipediaQueryRun
20
+ from langchain.utilities import WikipediaAPIWrapper
21
+ from langsmith.client import Client
22
+ from streamlit_feedback import streamlit_feedback
23
+
24
+ from defaults import default_values
25
+ from llm_resources import (
26
+ get_agent,
27
+ get_doc_agent,
28
+ get_llm,
29
+ get_runnable,
30
+ get_texts_and_multiretriever,
31
+ )
32
+ from python_coder import get_agent as get_python_agent
33
+ from research_assistant.chain import get_chain as get_research_assistant_chain
34
+
35
+ __version__ = "2.1.4"
36
+
37
+ # --- Initialization ---
38
+ st.set_page_config(
39
+ page_title=f"langchain-streamlit-demo v{__version__}",
40
+ page_icon="🦜",
41
+ )
42
+
43
+
44
+ def st_init_null(*variable_names) -> None:
45
+ for variable_name in variable_names:
46
+ if variable_name not in st.session_state:
47
+ st.session_state[variable_name] = None
48
+
49
+
50
+ st_init_null(
51
+ "chain",
52
+ "client",
53
+ "doc_chain",
54
+ "document_chat_chain_type",
55
+ "llm",
56
+ "ls_tracer",
57
+ "provider",
58
+ "retriever",
59
+ "run",
60
+ "run_id",
61
+ "trace_link",
62
+ "LANGSMITH_API_KEY",
63
+ "LANGSMITH_PROJECT",
64
+ "AZURE_OPENAI_BASE_URL",
65
+ "AZURE_OPENAI_API_VERSION",
66
+ "AZURE_OPENAI_DEPLOYMENT_NAME",
67
+ "AZURE_OPENAI_EMB_DEPLOYMENT_NAME",
68
+ "AZURE_OPENAI_API_KEY",
69
+ "AZURE_OPENAI_MODEL_VERSION",
70
+ "AZURE_AVAILABLE",
71
+ )
72
+
73
+ # --- LLM globals ---
74
+ STMEMORY = StreamlitChatMessageHistory(key="langchain_messages")
75
+ MEMORY = ConversationBufferMemory(
76
+ chat_memory=STMEMORY,
77
+ return_messages=True,
78
+ memory_key="chat_history",
79
+ )
80
+
81
+ RUN_COLLECTOR = RunCollectorCallbackHandler()
82
+
83
+ st.session_state.LANGSMITH_API_KEY = (
84
+ st.session_state.LANGSMITH_API_KEY
85
+ or default_values.PROVIDER_KEY_DICT.get("LANGSMITH")
86
+ )
87
+
88
+ st.session_state.LANGSMITH_PROJECT = st.session_state.LANGSMITH_PROJECT or (
89
+ default_values.DEFAULT_LANGSMITH_PROJECT or "langchain-streamlit-demo"
90
+ )
91
+
92
+
93
+ def azure_state_or_default(*args):
94
+ st.session_state.update(
95
+ {
96
+ arg: st.session_state.get(arg) or default_values.AZURE_DICT.get(arg)
97
+ for arg in args
98
+ },
99
+ )
100
+
101
+
102
+ azure_state_or_default(
103
+ "AZURE_OPENAI_BASE_URL",
104
+ "AZURE_OPENAI_API_VERSION",
105
+ "AZURE_OPENAI_DEPLOYMENT_NAME",
106
+ "AZURE_OPENAI_EMB_DEPLOYMENT_NAME",
107
+ "AZURE_OPENAI_API_KEY",
108
+ "AZURE_OPENAI_MODEL_VERSION",
109
+ )
110
+
111
+ st.session_state.AZURE_AVAILABLE = all(
112
+ [
113
+ st.session_state.AZURE_OPENAI_BASE_URL,
114
+ st.session_state.AZURE_OPENAI_API_VERSION,
115
+ st.session_state.AZURE_OPENAI_DEPLOYMENT_NAME,
116
+ st.session_state.AZURE_OPENAI_API_KEY,
117
+ st.session_state.AZURE_OPENAI_MODEL_VERSION,
118
+ ],
119
+ )
120
+
121
+ st.session_state.AZURE_EMB_AVAILABLE = (
122
+ st.session_state.AZURE_AVAILABLE
123
+ and st.session_state.AZURE_OPENAI_EMB_DEPLOYMENT_NAME
124
+ )
125
+
126
+ AZURE_KWARGS = (
127
+ None
128
+ if not st.session_state.AZURE_EMB_AVAILABLE
129
+ else {
130
+ "openai_api_base": st.session_state.AZURE_OPENAI_BASE_URL,
131
+ "openai_api_version": st.session_state.AZURE_OPENAI_API_VERSION,
132
+ "deployment": st.session_state.AZURE_OPENAI_EMB_DEPLOYMENT_NAME,
133
+ "openai_api_key": st.session_state.AZURE_OPENAI_API_KEY,
134
+ "openai_api_type": "azure",
135
+ }
136
+ )
137
+
138
+
139
+ @st.cache_resource
140
+ def get_texts_and_retriever_cacheable_wrapper(
141
+ uploaded_file_bytes: bytes,
142
+ openai_api_key: str,
143
+ chunk_size: int = default_values.DEFAULT_CHUNK_SIZE,
144
+ chunk_overlap: int = default_values.DEFAULT_CHUNK_OVERLAP,
145
+ k: int = default_values.DEFAULT_RETRIEVER_K,
146
+ azure_kwargs: Optional[Dict[str, str]] = None,
147
+ use_azure: bool = False,
148
+ ) -> Tuple[List[Document], BaseRetriever]:
149
+ return get_texts_and_multiretriever(
150
+ uploaded_file_bytes=uploaded_file_bytes,
151
+ openai_api_key=openai_api_key,
152
+ chunk_size=chunk_size,
153
+ chunk_overlap=chunk_overlap,
154
+ k=k,
155
+ azure_kwargs=azure_kwargs,
156
+ use_azure=use_azure,
157
+ )
158
+
159
+
160
+ # --- Sidebar ---
161
+ sidebar = st.sidebar
162
+ with sidebar:
163
+ st.markdown("# Menu")
164
+
165
+ model = st.selectbox(
166
+ label="Chat Model",
167
+ options=default_values.SUPPORTED_MODELS,
168
+ index=default_values.SUPPORTED_MODELS.index(default_values.DEFAULT_MODEL),
169
+ )
170
+
171
+ st.session_state.provider = default_values.MODEL_DICT[model]
172
+
173
+ provider_api_key = (
174
+ default_values.PROVIDER_KEY_DICT.get(
175
+ st.session_state.provider,
176
+ )
177
+ or st.text_input(
178
+ f"{st.session_state.provider} API key",
179
+ type="password",
180
+ )
181
+ if st.session_state.provider != "Azure OpenAI"
182
+ else ""
183
+ )
184
+
185
+ if st.button("Clear message history"):
186
+ STMEMORY.clear()
187
+ st.session_state.trace_link = None
188
+ st.session_state.run_id = None
189
+
190
+ # --- Document Chat Options ---
191
+ with st.expander("Document Chat", expanded=False):
192
+ uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
193
+
194
+ openai_api_key = (
195
+ provider_api_key
196
+ if st.session_state.provider == "OpenAI"
197
+ else default_values.OPENAI_API_KEY
198
+ or st.sidebar.text_input("OpenAI API Key: ", type="password")
199
+ )
200
+
201
+ document_chat = st.checkbox(
202
+ "Document Chat",
203
+ value=True if uploaded_file else False,
204
+ help="Uploaded document will provide context for the chat.",
205
+ )
206
+
207
+ k = st.slider(
208
+ label="Number of Chunks",
209
+ help="How many document chunks will be used for context?",
210
+ value=default_values.DEFAULT_RETRIEVER_K,
211
+ min_value=1,
212
+ max_value=10,
213
+ )
214
+
215
+ chunk_size = st.slider(
216
+ label="Number of Tokens per Chunk",
217
+ help="Size of each chunk of text",
218
+ min_value=default_values.MIN_CHUNK_SIZE,
219
+ max_value=default_values.MAX_CHUNK_SIZE,
220
+ value=default_values.DEFAULT_CHUNK_SIZE,
221
+ )
222
+
223
+ chunk_overlap = st.slider(
224
+ label="Chunk Overlap",
225
+ help="Number of characters to overlap between chunks",
226
+ min_value=default_values.MIN_CHUNK_OVERLAP,
227
+ max_value=default_values.MAX_CHUNK_OVERLAP,
228
+ value=default_values.DEFAULT_CHUNK_OVERLAP,
229
+ )
230
+
231
+ chain_type_help_root = (
232
+ "https://python.langchain.com/docs/modules/chains/document/"
233
+ )
234
+
235
+ chain_type_help = "\n".join(
236
+ f"- [{chain_type_name}]({chain_type_help_root}/{chain_type_name})"
237
+ for chain_type_name in (
238
+ "stuff",
239
+ "refine",
240
+ "map_reduce",
241
+ "map_rerank",
242
+ )
243
+ )
244
+
245
+ document_chat_chain_type = st.selectbox(
246
+ label="Document Chat Chain Type",
247
+ options=[
248
+ "stuff",
249
+ "refine",
250
+ "map_reduce",
251
+ "map_rerank",
252
+ "Q&A Generation",
253
+ "Summarization",
254
+ ],
255
+ index=0,
256
+ help=chain_type_help,
257
+ )
258
+ use_azure = st.toggle(
259
+ label="Use Azure OpenAI",
260
+ value=st.session_state.AZURE_EMB_AVAILABLE,
261
+ help="Use Azure for embeddings instead of using OpenAI directly.",
262
+ )
263
+
264
+ if uploaded_file:
265
+ if st.session_state.AZURE_EMB_AVAILABLE or openai_api_key:
266
+ (
267
+ st.session_state.texts,
268
+ st.session_state.retriever,
269
+ ) = get_texts_and_retriever_cacheable_wrapper(
270
+ uploaded_file_bytes=uploaded_file.getvalue(),
271
+ openai_api_key=openai_api_key,
272
+ chunk_size=chunk_size,
273
+ chunk_overlap=chunk_overlap,
274
+ k=k,
275
+ azure_kwargs=AZURE_KWARGS,
276
+ use_azure=use_azure,
277
+ )
278
+ else:
279
+ st.error("Please enter a valid OpenAI API key.", icon="❌")
280
+
281
+ # --- Advanced Settings ---
282
+ with st.expander("Advanced Settings", expanded=False):
283
+ st.markdown("## Feedback Scale")
284
+ use_faces = st.toggle(label="`Thumbs` ⇄ `Faces`", value=False)
285
+ feedback_option = "faces" if use_faces else "thumbs"
286
+
287
+ system_prompt = (
288
+ st.text_area(
289
+ "Custom Instructions",
290
+ default_values.DEFAULT_SYSTEM_PROMPT,
291
+ help="Custom instructions to provide the language model to determine style, personality, etc.",
292
+ )
293
+ .strip()
294
+ .replace("{", "{{")
295
+ .replace("}", "}}")
296
+ )
297
+
298
+ temperature = st.slider(
299
+ "Temperature",
300
+ min_value=default_values.MIN_TEMP,
301
+ max_value=default_values.MAX_TEMP,
302
+ value=default_values.DEFAULT_TEMP,
303
+ help="Higher values give more random results.",
304
+ )
305
+
306
+ max_tokens = st.slider(
307
+ "Max Tokens",
308
+ min_value=default_values.MIN_MAX_TOKENS,
309
+ max_value=default_values.MAX_MAX_TOKENS,
310
+ value=default_values.DEFAULT_MAX_TOKENS,
311
+ help="Higher values give longer results.",
312
+ )
313
+
314
+ # --- LangSmith Options ---
315
+ if default_values.SHOW_LANGSMITH_OPTIONS:
316
+ with st.expander("LangSmith Options", expanded=False):
317
+ st.session_state.LANGSMITH_API_KEY = st.text_input(
318
+ "LangSmith API Key (optional)",
319
+ value=st.session_state.LANGSMITH_API_KEY,
320
+ type="password",
321
+ )
322
+
323
+ st.session_state.LANGSMITH_PROJECT = st.text_input(
324
+ "LangSmith Project Name",
325
+ value=st.session_state.LANGSMITH_PROJECT,
326
+ )
327
+
328
+ if st.session_state.client is None and st.session_state.LANGSMITH_API_KEY:
329
+ st.session_state.client = Client(
330
+ api_url="https://api.smith.langchain.com",
331
+ api_key=st.session_state.LANGSMITH_API_KEY,
332
+ )
333
+ st.session_state.ls_tracer = LangChainTracer(
334
+ project_name=st.session_state.LANGSMITH_PROJECT,
335
+ client=st.session_state.client,
336
+ )
337
+
338
+ # --- Azure Options ---
339
+ if default_values.SHOW_AZURE_OPTIONS:
340
+ with st.expander("Azure Options", expanded=False):
341
+ st.session_state.AZURE_OPENAI_BASE_URL = st.text_input(
342
+ "AZURE_OPENAI_BASE_URL",
343
+ value=st.session_state.AZURE_OPENAI_BASE_URL,
344
+ )
345
+
346
+ st.session_state.AZURE_OPENAI_API_VERSION = st.text_input(
347
+ "AZURE_OPENAI_API_VERSION",
348
+ value=st.session_state.AZURE_OPENAI_API_VERSION,
349
+ )
350
+
351
+ st.session_state.AZURE_OPENAI_DEPLOYMENT_NAME = st.text_input(
352
+ "AZURE_OPENAI_DEPLOYMENT_NAME",
353
+ value=st.session_state.AZURE_OPENAI_DEPLOYMENT_NAME,
354
+ )
355
+
356
+ st.session_state.AZURE_OPENAI_EMB_DEPLOYMENT_NAME = st.text_input(
357
+ "AZURE_OPENAI_EMB_DEPLOYMENT_NAME",
358
+ value=st.session_state.AZURE_OPENAI_EMB_DEPLOYMENT_NAME,
359
+ )
360
+
361
+ st.session_state.AZURE_OPENAI_API_KEY = st.text_input(
362
+ "AZURE_OPENAI_API_KEY",
363
+ value=st.session_state.AZURE_OPENAI_API_KEY,
364
+ type="password",
365
+ )
366
+
367
+ st.session_state.AZURE_OPENAI_MODEL_VERSION = st.text_input(
368
+ "AZURE_OPENAI_MODEL_VERSION",
369
+ value=st.session_state.AZURE_OPENAI_MODEL_VERSION,
370
+ )
371
+
372
+
373
+ # --- LLM Instantiation ---
374
+ get_llm_args = dict(
375
+ provider=st.session_state.provider,
376
+ model=model,
377
+ provider_api_key=provider_api_key,
378
+ temperature=temperature,
379
+ max_tokens=max_tokens,
380
+ azure_available=st.session_state.AZURE_AVAILABLE,
381
+ azure_dict={
382
+ "AZURE_OPENAI_BASE_URL": st.session_state.AZURE_OPENAI_BASE_URL,
383
+ "AZURE_OPENAI_API_VERSION": st.session_state.AZURE_OPENAI_API_VERSION,
384
+ "AZURE_OPENAI_DEPLOYMENT_NAME": st.session_state.AZURE_OPENAI_DEPLOYMENT_NAME,
385
+ "AZURE_OPENAI_API_KEY": st.session_state.AZURE_OPENAI_API_KEY,
386
+ "AZURE_OPENAI_MODEL_VERSION": st.session_state.AZURE_OPENAI_MODEL_VERSION,
387
+ },
388
+ )
389
+ get_llm_args_temp_zero = get_llm_args | {"temperature": 0.0}
390
+ st.session_state.llm = get_llm(**get_llm_args)
391
+
392
+ # --- Chat History ---
393
+ for msg in STMEMORY.messages:
394
+ if msg.content and msg.type in ("ai", "assistant", "human", "user"):
395
+ st.chat_message(
396
+ msg.type,
397
+ avatar="🦜" if msg.type in ("ai", "assistant") else None,
398
+ ).write(msg.content)
399
+
400
+
401
+ # --- Current Chat ---
402
+ if st.session_state.llm:
403
+ # --- Regular Chat ---
404
+ chat_prompt = ChatPromptTemplate.from_messages(
405
+ [
406
+ (
407
+ "system",
408
+ system_prompt + "\nIt's currently {time}.",
409
+ ),
410
+ MessagesPlaceholder(variable_name="chat_history"),
411
+ ("human", "{query}"),
412
+ ],
413
+ ).partial(time=lambda: str(datetime.now()))
414
+
415
+ # --- Chat Input ---
416
+ prompt = st.chat_input(placeholder="Ask me a question!")
417
+ if prompt:
418
+ st.chat_message("user").write(prompt)
419
+ feedback_update = None
420
+ feedback = None
421
+
422
+ # --- Chat Output ---
423
+ with st.chat_message("assistant", avatar="🦜"):
424
+ callbacks = [RUN_COLLECTOR]
425
+
426
+ if st.session_state.ls_tracer:
427
+ callbacks.append(st.session_state.ls_tracer)
428
+
429
+ def get_config(callbacks: list[BaseCallbackHandler]) -> dict[str, Any]:
430
+ config: Dict[str, Any] = dict(
431
+ callbacks=callbacks,
432
+ tags=["Streamlit Chat"],
433
+ verbose=True,
434
+ return_intermediate_steps=False,
435
+ )
436
+ if st.session_state.provider == "Anthropic":
437
+ config["max_concurrency"] = 5
438
+ return config
439
+
440
+ use_document_chat = all(
441
+ [
442
+ document_chat,
443
+ st.session_state.retriever,
444
+ ],
445
+ )
446
+
447
+ full_response: Union[str, None] = None
448
+ # stream_handler = StreamHandler(message_placeholder)
449
+ # callbacks.append(stream_handler)
450
+ message_placeholder = st.empty()
451
+ default_tools = [
452
+ DuckDuckGoSearchRun(),
453
+ WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()),
454
+ ]
455
+ default_tools += load_tools(["requests_get"])
456
+ default_tools += load_tools(["llm-math"], llm=st.session_state.llm)
457
+ if st.session_state.provider in ("Azure OpenAI", "OpenAI"):
458
+ research_assistant_chain = get_research_assistant_chain(
459
+ search_llm=get_llm(**get_llm_args_temp_zero), # type: ignore
460
+ writer_llm=get_llm(**get_llm_args_temp_zero), # type: ignore
461
+ )
462
+ st_callback = StreamlitCallbackHandler(st.container())
463
+ callbacks.append(st_callback)
464
+
465
+ @tool("web-research-assistant")
466
+ def research_assistant_tool(question: str, callbacks: Callbacks = None):
467
+ """This assistant returns a comprehensive report based on web research.
468
+ It's slow and relatively expensive, so use it sparingly.
469
+ Consider using a different tool for quick facts or web queries.
470
+ """
471
+ return research_assistant_chain.invoke(
472
+ dict(question=question),
473
+ config=get_config(callbacks),
474
+ )
475
+
476
+ python_coder_agent = get_python_agent(st.session_state.llm)
477
+
478
+ @tool("python-coder-assistant")
479
+ def python_coder_tool(input_str: str, callbacks: Callbacks = None):
480
+ """This assistant writes PYTHON code.
481
+ Give it clear instructions and requirements.
482
+ Do not use it for tasks other than Python.
483
+ """
484
+ return python_coder_agent.invoke(
485
+ dict(input=input_str),
486
+ config=get_config(callbacks),
487
+ )
488
+
489
+ TOOLS = [research_assistant_tool, python_coder_tool] + default_tools
490
+
491
+ if use_document_chat:
492
+ st.session_state.doc_chain = get_runnable(
493
+ use_document_chat,
494
+ document_chat_chain_type,
495
+ st.session_state.llm,
496
+ st.session_state.retriever,
497
+ MEMORY,
498
+ chat_prompt,
499
+ prompt,
500
+ )
501
+
502
+ @tool("user-document-chat")
503
+ def doc_chain_tool(input_str: str, callbacks: Callbacks = None):
504
+ """Always use this tool at least once. Input should be a question."""
505
+ return st.session_state.doc_chain.invoke(
506
+ input_str,
507
+ config=get_config(callbacks),
508
+ )
509
+
510
+ doc_chain_agent = get_doc_agent(
511
+ [doc_chain_tool],
512
+ )
513
+
514
+ @tool("document-question-tool")
515
+ def doc_question_tool(input_str: str, callbacks: Callbacks = None):
516
+ """This tool is an AI assistant with access to the user's uploaded document.
517
+ Input should be one or more questions, requests, instructions, etc.
518
+ If the user's meaning is unclear, perhaps the answer is here.
519
+ Generally speaking, try this tool before conducting web research.
520
+ """
521
+ return doc_chain_agent.invoke(
522
+ input_str,
523
+ config=get_config(callbacks),
524
+ )
525
+
526
+ TOOLS = [doc_question_tool] + TOOLS
527
+
528
+ st.session_state.chain = get_agent(
529
+ TOOLS,
530
+ STMEMORY,
531
+ st.session_state.llm,
532
+ callbacks,
533
+ )
534
+ else:
535
+ st.session_state.chain = get_runnable(
536
+ use_document_chat,
537
+ document_chat_chain_type,
538
+ st.session_state.llm,
539
+ st.session_state.retriever,
540
+ MEMORY,
541
+ chat_prompt,
542
+ prompt,
543
+ )
544
+
545
+ # --- LLM call ---
546
+ try:
547
+ full_response = st.session_state.chain.invoke(
548
+ prompt,
549
+ config=get_config(callbacks),
550
+ )
551
+
552
+ except (openai.AuthenticationError, anthropic.AuthenticationError):
553
+ st.error(
554
+ f"Please enter a valid {st.session_state.provider} API key.",
555
+ icon="❌",
556
+ )
557
+
558
+ # --- Display output ---
559
+ if full_response is not None:
560
+ message_placeholder.markdown(full_response)
561
+
562
+ # --- Tracing ---
563
+ if st.session_state.client:
564
+ st.session_state.run = RUN_COLLECTOR.traced_runs[0]
565
+ st.session_state.run_id = st.session_state.run.id
566
+ RUN_COLLECTOR.traced_runs = []
567
+ wait_for_all_tracers()
568
+ try:
569
+ st.session_state.trace_link = st.session_state.client.read_run(
570
+ st.session_state.run_id,
571
+ ).url
572
+ except (
573
+ langsmith.utils.LangSmithError,
574
+ langsmith.utils.LangSmithNotFoundError,
575
+ ):
576
+ st.session_state.trace_link = None
577
+
578
+ # --- LangSmith Trace Link ---
579
+ if st.session_state.trace_link:
580
+ with sidebar:
581
+ st.markdown(
582
+ f'<a href="{st.session_state.trace_link}" target="_blank"><button>Latest Trace: 🛠️</button></a>',
583
+ unsafe_allow_html=True,
584
+ )
585
+
586
+ # --- Feedback ---
587
+ if st.session_state.client and st.session_state.run_id:
588
+ feedback = streamlit_feedback(
589
+ feedback_type=feedback_option,
590
+ optional_text_label="[Optional] Please provide an explanation",
591
+ key=f"feedback_{st.session_state.run_id}",
592
+ )
593
+
594
+ # Define score mappings for both "thumbs" and "faces" feedback systems
595
+ score_mappings: dict[str, dict[str, Union[int, float]]] = {
596
+ "thumbs": {"👍": 1, "👎": 0},
597
+ "faces": {"😀": 1, "🙂": 0.75, "😐": 0.5, "🙁": 0.25, "😞": 0},
598
+ }
599
+
600
+ # Get the score mapping based on the selected feedback option
601
+ scores = score_mappings[feedback_option]
602
+
603
+ if feedback:
604
+ # Get the score from the selected feedback option's score mapping
605
+ score = scores.get(
606
+ feedback["score"],
607
+ )
608
+
609
+ if score is not None:
610
+ # Formulate feedback type string incorporating the feedback option
611
+ # and score value
612
+ feedback_type_str = f"{feedback_option} {feedback['score']}"
613
+
614
+ # Record the feedback with the formulated feedback type string
615
+ # and optional comment
616
+ feedback_record = st.session_state.client.create_feedback(
617
+ st.session_state.run_id,
618
+ feedback_type_str,
619
+ score=score,
620
+ comment=feedback.get("text"),
621
+ )
622
+ st.toast("Feedback recorded!", icon="📝")
623
+ else:
624
+ st.warning("Invalid feedback score.")
625
+
626
+ else:
627
+ st.error(f"Please enter a valid {st.session_state.provider} API key.", icon="❌")
langchain-streamlit-demo/defaults.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from collections import namedtuple
3
+
4
+
5
+ MODEL_DICT = {
6
+ "gpt-3.5-turbo": "OpenAI",
7
+ "gpt-4": "OpenAI",
8
+ "gpt-4-1106-preview": "OpenAI",
9
+ "claude-instant-1": "Anthropic",
10
+ "claude-instant-1.2": "Anthropic",
11
+ "claude-2": "Anthropic",
12
+ "claude-2.1": "Anthropic",
13
+ "meta-llama/Llama-2-7b-chat-hf": "Anyscale Endpoints",
14
+ "meta-llama/Llama-2-13b-chat-hf": "Anyscale Endpoints",
15
+ "meta-llama/Llama-2-70b-chat-hf": "Anyscale Endpoints",
16
+ "codellama/CodeLlama-34b-Instruct-hf": "Anyscale Endpoints",
17
+ "mistralai/Mistral-7B-Instruct-v0.1": "Anyscale Endpoints",
18
+ "HuggingFaceH4/zephyr-7b-beta": "Anyscale Endpoints",
19
+ "Azure OpenAI": "Azure OpenAI",
20
+ }
21
+
22
+ SUPPORTED_MODELS = list(MODEL_DICT.keys())
23
+
24
+ DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gpt-3.5-turbo")
25
+
26
+ DEFAULT_SYSTEM_PROMPT = os.environ.get(
27
+ "DEFAULT_SYSTEM_PROMPT",
28
+ "You are a helpful chatbot. Do not rush. Always plan, think, and act in a step-by-step manner.",
29
+ )
30
+
31
+ MIN_TEMP = float(os.environ.get("MIN_TEMPERATURE", 0.0))
32
+ MAX_TEMP = float(os.environ.get("MAX_TEMPERATURE", 1.0))
33
+ DEFAULT_TEMP = float(os.environ.get("DEFAULT_TEMPERATURE", 0.7))
34
+
35
+ MIN_MAX_TOKENS = int(os.environ.get("MIN_MAX_TOKENS", 1))
36
+ MAX_MAX_TOKENS = int(os.environ.get("MAX_MAX_TOKENS", 100000))
37
+ DEFAULT_MAX_TOKENS = int(os.environ.get("DEFAULT_MAX_TOKENS", 1000))
38
+
39
+ DEFAULT_LANGSMITH_PROJECT = os.environ.get("LANGCHAIN_PROJECT")
40
+
41
+ AZURE_VARS = [
42
+ "AZURE_OPENAI_BASE_URL",
43
+ "AZURE_OPENAI_API_VERSION",
44
+ "AZURE_OPENAI_DEPLOYMENT_NAME",
45
+ "AZURE_OPENAI_EMB_DEPLOYMENT_NAME",
46
+ "AZURE_OPENAI_API_KEY",
47
+ "AZURE_OPENAI_MODEL_VERSION",
48
+ ]
49
+
50
+ AZURE_DICT = {v: os.environ.get(v, "") for v in AZURE_VARS}
51
+
52
+
53
+ SHOW_LANGSMITH_OPTIONS = (
54
+ os.environ.get("SHOW_LANGSMITH_OPTIONS", "true").lower() == "true"
55
+ )
56
+ SHOW_AZURE_OPTIONS = os.environ.get("SHOW_AZURE_OPTIONS", "true").lower() == "true"
57
+
58
+ PROVIDER_KEY_DICT = {
59
+ "OpenAI": os.environ.get("OPENAI_API_KEY", ""),
60
+ "Anthropic": os.environ.get("ANTHROPIC_API_KEY", ""),
61
+ "Anyscale Endpoints": os.environ.get("ANYSCALE_API_KEY", ""),
62
+ "LANGSMITH": os.environ.get("LANGCHAIN_API_KEY", ""),
63
+ }
64
+
65
+ OPENAI_API_KEY = PROVIDER_KEY_DICT["OpenAI"]
66
+
67
+
68
+ MIN_CHUNK_SIZE = 1
69
+ MAX_CHUNK_SIZE = 10000
70
+ DEFAULT_CHUNK_SIZE = 1000
71
+
72
+ MIN_CHUNK_OVERLAP = 0
73
+ MAX_CHUNK_OVERLAP = 10000
74
+ DEFAULT_CHUNK_OVERLAP = 0
75
+
76
+ DEFAULT_RETRIEVER_K = 4
77
+
78
+ DEFAULT_VALUES = namedtuple(
79
+ "DEFAULT_VALUES",
80
+ [
81
+ "MODEL_DICT",
82
+ "SUPPORTED_MODELS",
83
+ "DEFAULT_MODEL",
84
+ "DEFAULT_SYSTEM_PROMPT",
85
+ "MIN_TEMP",
86
+ "MAX_TEMP",
87
+ "DEFAULT_TEMP",
88
+ "MIN_MAX_TOKENS",
89
+ "MAX_MAX_TOKENS",
90
+ "DEFAULT_MAX_TOKENS",
91
+ "DEFAULT_LANGSMITH_PROJECT",
92
+ "AZURE_VARS",
93
+ "AZURE_DICT",
94
+ "PROVIDER_KEY_DICT",
95
+ "OPENAI_API_KEY",
96
+ "MIN_CHUNK_SIZE",
97
+ "MAX_CHUNK_SIZE",
98
+ "DEFAULT_CHUNK_SIZE",
99
+ "MIN_CHUNK_OVERLAP",
100
+ "MAX_CHUNK_OVERLAP",
101
+ "DEFAULT_CHUNK_OVERLAP",
102
+ "DEFAULT_RETRIEVER_K",
103
+ "SHOW_LANGSMITH_OPTIONS",
104
+ "SHOW_AZURE_OPTIONS",
105
+ ],
106
+ )
107
+
108
+
109
+ default_values = DEFAULT_VALUES(
110
+ MODEL_DICT,
111
+ SUPPORTED_MODELS,
112
+ DEFAULT_MODEL,
113
+ DEFAULT_SYSTEM_PROMPT,
114
+ MIN_TEMP,
115
+ MAX_TEMP,
116
+ DEFAULT_TEMP,
117
+ MIN_MAX_TOKENS,
118
+ MAX_MAX_TOKENS,
119
+ DEFAULT_MAX_TOKENS,
120
+ DEFAULT_LANGSMITH_PROJECT,
121
+ AZURE_VARS,
122
+ AZURE_DICT,
123
+ PROVIDER_KEY_DICT,
124
+ OPENAI_API_KEY,
125
+ MIN_CHUNK_SIZE,
126
+ MAX_CHUNK_SIZE,
127
+ DEFAULT_CHUNK_SIZE,
128
+ MIN_CHUNK_OVERLAP,
129
+ MAX_CHUNK_OVERLAP,
130
+ DEFAULT_CHUNK_OVERLAP,
131
+ DEFAULT_RETRIEVER_K,
132
+ SHOW_LANGSMITH_OPTIONS,
133
+ SHOW_AZURE_OPTIONS,
134
+ )
langchain-streamlit-demo/llm_resources.py ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ from tempfile import NamedTemporaryFile
3
+ from typing import Tuple, List, Optional, Dict
4
+
5
+ from langchain.agents import AgentExecutor, AgentType, initialize_agent
6
+ from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
7
+ from langchain.callbacks.base import BaseCallbackHandler
8
+ from langchain.chains import LLMChain
9
+ from langchain.chains import RetrievalQA
10
+ from langchain.chat_models import (
11
+ AzureChatOpenAI,
12
+ ChatOpenAI,
13
+ ChatAnthropic,
14
+ ChatAnyscale,
15
+ )
16
+ from langchain.document_loaders import PyPDFLoader
17
+ from langchain.embeddings import AzureOpenAIEmbeddings, OpenAIEmbeddings
18
+ from langchain.llms.base import BaseLLM
19
+ from langchain.memory import ConversationBufferMemory
20
+ from langchain.prompts import MessagesPlaceholder, ChatPromptTemplate
21
+ from langchain.retrievers import EnsembleRetriever
22
+ from langchain.retrievers.multi_query import MultiQueryRetriever
23
+ from langchain.retrievers.multi_vector import MultiVectorRetriever
24
+ from langchain.schema import Document, BaseRetriever
25
+ from langchain.schema.chat_history import BaseChatMessageHistory
26
+ from langchain.schema.runnable import RunnablePassthrough
27
+ from langchain.storage import InMemoryStore
28
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
29
+ from langchain.tools.base import BaseTool
30
+ from langchain.vectorstores import FAISS
31
+ from langchain_core.messages import SystemMessage
32
+
33
+ from defaults import DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_OVERLAP, DEFAULT_RETRIEVER_K
34
+ from qagen import get_rag_qa_gen_chain
35
+ from summarize import get_rag_summarization_chain
36
+
37
+
38
+ def get_agent(
39
+ tools: list[BaseTool],
40
+ chat_history: BaseChatMessageHistory,
41
+ llm: BaseLLM,
42
+ callbacks,
43
+ ):
44
+ memory_key = "agent_history"
45
+ system_message = SystemMessage(
46
+ content=(
47
+ "Do your best to answer the questions. "
48
+ "Feel free to use any tools available to look up "
49
+ "relevant information, only if necessary"
50
+ ),
51
+ )
52
+ prompt = OpenAIFunctionsAgent.create_prompt(
53
+ system_message=system_message,
54
+ extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],
55
+ )
56
+ agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
57
+
58
+ # agent_memory = AgentTokenBufferMemory(
59
+ # chat_memory=chat_history,
60
+ # memory_key=memory_key,
61
+ # llm=llm,
62
+ # )
63
+
64
+ agent_memory = ConversationBufferMemory(
65
+ chat_memory=chat_history,
66
+ return_messages=True,
67
+ memory_key=memory_key,
68
+ )
69
+
70
+ agent_executor = AgentExecutor(
71
+ agent=agent,
72
+ tools=tools,
73
+ memory=agent_memory,
74
+ verbose=True,
75
+ return_intermediate_steps=False,
76
+ callbacks=callbacks,
77
+ )
78
+ return (
79
+ {"input": RunnablePassthrough()}
80
+ | agent_executor
81
+ | (lambda output: output["output"])
82
+ )
83
+
84
+
85
+ def get_doc_agent(
86
+ tools: list[BaseTool],
87
+ llm: Optional[BaseLLM] = None,
88
+ agent_type: AgentType = AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
89
+ ):
90
+ if llm is None:
91
+ llm = ChatOpenAI(
92
+ model_name="gpt-4-1106-preview",
93
+ temperature=0.0,
94
+ streaming=True,
95
+ )
96
+ prompt = ChatPromptTemplate.from_messages(
97
+ [
98
+ (
99
+ "system",
100
+ """
101
+ You assist a chatbot with answering questions about a document.
102
+ If necessary, break up incoming questions into multiple parts,
103
+ and use the tools provided to answer smaller questions before
104
+ answering the larger question.
105
+ """,
106
+ ),
107
+ ("user", "{input}"),
108
+ MessagesPlaceholder(variable_name="agent_scratchpad"),
109
+ ],
110
+ )
111
+ agent_executor = initialize_agent(
112
+ tools,
113
+ llm,
114
+ agent=agent_type,
115
+ verbose=True,
116
+ memory=None,
117
+ handle_parsing_errors=True,
118
+ prompt=prompt,
119
+ )
120
+ return (
121
+ {"input": RunnablePassthrough()}
122
+ | agent_executor
123
+ | (lambda output: output["output"])
124
+ )
125
+
126
+
127
+ def get_runnable(
128
+ use_document_chat: bool,
129
+ document_chat_chain_type: str,
130
+ llm,
131
+ retriever,
132
+ memory,
133
+ chat_prompt,
134
+ summarization_prompt,
135
+ ):
136
+ if not use_document_chat:
137
+ return LLMChain(
138
+ prompt=chat_prompt,
139
+ llm=llm,
140
+ memory=memory,
141
+ ) | (lambda output: output["text"])
142
+
143
+ if document_chat_chain_type == "Q&A Generation":
144
+ return get_rag_qa_gen_chain(
145
+ retriever,
146
+ llm,
147
+ )
148
+ elif document_chat_chain_type == "Summarization":
149
+ return get_rag_summarization_chain(
150
+ summarization_prompt,
151
+ retriever,
152
+ llm,
153
+ )
154
+ else:
155
+ return RetrievalQA.from_chain_type(
156
+ llm=llm,
157
+ chain_type=document_chat_chain_type,
158
+ retriever=retriever,
159
+ output_key="output_text",
160
+ ) | (lambda output: output["output_text"])
161
+
162
+
163
+ def get_llm(
164
+ provider: str,
165
+ model: str,
166
+ provider_api_key: str,
167
+ temperature: float,
168
+ max_tokens: int,
169
+ azure_available: bool,
170
+ azure_dict: dict[str, str],
171
+ ):
172
+ if azure_available and provider == "Azure OpenAI":
173
+ return AzureChatOpenAI(
174
+ azure_endpoint=azure_dict["AZURE_OPENAI_BASE_URL"],
175
+ openai_api_version=azure_dict["AZURE_OPENAI_API_VERSION"],
176
+ deployment_name=azure_dict["AZURE_OPENAI_DEPLOYMENT_NAME"],
177
+ openai_api_key=azure_dict["AZURE_OPENAI_API_KEY"],
178
+ openai_api_type="azure",
179
+ model_version=azure_dict["AZURE_OPENAI_MODEL_VERSION"],
180
+ temperature=temperature,
181
+ streaming=True,
182
+ max_tokens=max_tokens,
183
+ )
184
+
185
+ elif provider_api_key:
186
+ if provider == "OpenAI":
187
+ return ChatOpenAI(
188
+ model_name=model,
189
+ openai_api_key=provider_api_key,
190
+ temperature=temperature,
191
+ streaming=True,
192
+ max_tokens=max_tokens,
193
+ )
194
+
195
+ elif provider == "Anthropic":
196
+ return ChatAnthropic(
197
+ model=model,
198
+ anthropic_api_key=provider_api_key,
199
+ temperature=temperature,
200
+ streaming=True,
201
+ max_tokens_to_sample=max_tokens,
202
+ )
203
+
204
+ elif provider == "Anyscale Endpoints":
205
+ return ChatAnyscale(
206
+ model_name=model,
207
+ anyscale_api_key=provider_api_key,
208
+ temperature=temperature,
209
+ streaming=True,
210
+ max_tokens=max_tokens,
211
+ )
212
+
213
+ return None
214
+
215
+
216
+ def get_texts_and_multiretriever(
217
+ uploaded_file_bytes: bytes,
218
+ openai_api_key: str,
219
+ chunk_size: int = DEFAULT_CHUNK_SIZE,
220
+ chunk_overlap: int = DEFAULT_CHUNK_OVERLAP,
221
+ k: int = DEFAULT_RETRIEVER_K,
222
+ azure_kwargs: Optional[Dict[str, str]] = None,
223
+ use_azure: bool = False,
224
+ ) -> Tuple[List[Document], BaseRetriever]:
225
+ with NamedTemporaryFile() as temp_file:
226
+ temp_file.write(uploaded_file_bytes)
227
+ temp_file.seek(0)
228
+
229
+ loader = PyPDFLoader(temp_file.name)
230
+ documents = loader.load()
231
+ text_splitter = RecursiveCharacterTextSplitter(
232
+ chunk_size=10000,
233
+ chunk_overlap=0,
234
+ )
235
+ child_text_splitter = RecursiveCharacterTextSplitter(chunk_size=400)
236
+
237
+ texts = text_splitter.split_documents(documents)
238
+ id_key = "doc_id"
239
+
240
+ text_ids = [str(uuid.uuid4()) for _ in texts]
241
+ sub_texts = []
242
+ for i, text in enumerate(texts):
243
+ _id = text_ids[i]
244
+ _sub_texts = child_text_splitter.split_documents([text])
245
+ for _text in _sub_texts:
246
+ _text.metadata[id_key] = _id
247
+ sub_texts.extend(_sub_texts)
248
+
249
+ embeddings_kwargs = {"openai_api_key": openai_api_key}
250
+ if use_azure and azure_kwargs:
251
+ azure_kwargs["azure_endpoint"] = azure_kwargs.pop("openai_api_base")
252
+ embeddings_kwargs.update(azure_kwargs)
253
+ embeddings = AzureOpenAIEmbeddings(**embeddings_kwargs)
254
+ else:
255
+ embeddings = OpenAIEmbeddings(**embeddings_kwargs)
256
+ store = InMemoryStore()
257
+
258
+ # MultiVectorRetriever
259
+ multivectorstore = FAISS.from_documents(sub_texts, embeddings)
260
+ multivector_retriever = MultiVectorRetriever(
261
+ vectorstore=multivectorstore,
262
+ docstore=store,
263
+ id_key=id_key,
264
+ )
265
+ multivector_retriever.docstore.mset(list(zip(text_ids, texts)))
266
+ # multivector_retriever.k = k
267
+
268
+ multiquery_text_splitter = RecursiveCharacterTextSplitter(
269
+ chunk_size=chunk_size,
270
+ chunk_overlap=chunk_overlap,
271
+ )
272
+ # MultiQueryRetriever
273
+ multiquery_texts = multiquery_text_splitter.split_documents(documents)
274
+ multiquerystore = FAISS.from_documents(multiquery_texts, embeddings)
275
+ multiquery_retriever = MultiQueryRetriever.from_llm(
276
+ retriever=multiquerystore.as_retriever(search_kwargs={"k": k}),
277
+ llm=ChatOpenAI(),
278
+ )
279
+
280
+ ensemble_retriever = EnsembleRetriever(
281
+ retrievers=[multiquery_retriever, multivector_retriever],
282
+ weights=[0.5, 0.5],
283
+ )
284
+ return multiquery_texts, ensemble_retriever
285
+
286
+
287
+ class StreamHandler(BaseCallbackHandler):
288
+ def __init__(self, container, initial_text=""):
289
+ self.container = container
290
+ self.text = initial_text
291
+
292
+ def on_llm_new_token(self, token: str, **kwargs) -> None:
293
+ self.text += token
294
+ self.container.markdown(self.text)
langchain-streamlit-demo/python_coder.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """langchain python coder-- requires black, ruff, and mypy."""
2
+
3
+ import os
4
+ import re
5
+ import subprocess # nosec
6
+ import tempfile
7
+
8
+ from langchain.agents import initialize_agent, AgentType
9
+ from langchain.agents.tools import Tool
10
+ from langchain.llms.base import BaseLLM
11
+ from langchain.memory import ConversationBufferMemory
12
+ from langchain.prompts import ChatPromptTemplate
13
+ from langchain.prompts import MessagesPlaceholder
14
+ from langchain.pydantic_v1 import BaseModel, validator, Field, ValidationError
15
+
16
+
17
+ def strip_python_markdown_tags(text: str) -> str:
18
+ pat = re.compile(r"```python\n(.*)```", re.DOTALL)
19
+ code = pat.match(text)
20
+ if code:
21
+ return code.group(1)
22
+ else:
23
+ return text
24
+
25
+
26
+ def format_black(filepath: str):
27
+ """Format a file with black."""
28
+ subprocess.run( # nosec
29
+ f"black {filepath}",
30
+ stderr=subprocess.STDOUT,
31
+ text=True,
32
+ shell=True,
33
+ timeout=30,
34
+ check=False,
35
+ )
36
+
37
+
38
+ def format_ruff(filepath: str):
39
+ """Run ruff format on a file."""
40
+ subprocess.run( # nosec
41
+ f"ruff check --no-cache --fix {filepath}",
42
+ shell=True,
43
+ text=True,
44
+ timeout=30,
45
+ universal_newlines=True,
46
+ check=False,
47
+ )
48
+
49
+ subprocess.run( # nosec
50
+ f"ruff format --no-cache {filepath}",
51
+ stderr=subprocess.STDOUT,
52
+ shell=True,
53
+ timeout=30,
54
+ text=True,
55
+ check=False,
56
+ )
57
+
58
+
59
+ def check_ruff(filepath: str):
60
+ """Run ruff check on a file."""
61
+ subprocess.check_output( # nosec
62
+ f"ruff check --no-cache {filepath}",
63
+ stderr=subprocess.STDOUT,
64
+ shell=True,
65
+ timeout=30,
66
+ text=True,
67
+ )
68
+
69
+
70
+ def check_mypy(filepath: str, strict: bool = False, follow_imports: str = "skip"):
71
+ """Run mypy on a file."""
72
+ cmd = f"mypy {'--strict' if strict else ''} --follow-imports={follow_imports} {filepath}"
73
+
74
+ subprocess.check_output( # nosec
75
+ cmd,
76
+ stderr=subprocess.STDOUT,
77
+ shell=True,
78
+ text=True,
79
+ timeout=30,
80
+ )
81
+
82
+
83
+ class PythonCode(BaseModel):
84
+ code: str = Field(
85
+ description="Python code conforming to ruff, black, and *strict* mypy standards.",
86
+ )
87
+
88
+ @validator("code")
89
+ @classmethod
90
+ def check_code(cls, v: str) -> str:
91
+ v = strip_python_markdown_tags(v).strip()
92
+ try:
93
+ with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file:
94
+ temp_file.write(v)
95
+ temp_file_path = temp_file.name
96
+
97
+ try:
98
+ # format with black and ruff
99
+ format_black(temp_file_path)
100
+ format_ruff(temp_file_path)
101
+ except subprocess.CalledProcessError:
102
+ pass
103
+
104
+ # update `v` with formatted code
105
+ with open(temp_file_path, "r") as temp_file:
106
+ v = temp_file.read()
107
+
108
+ # check
109
+ complaints = dict(ruff=None, mypy=None)
110
+
111
+ try:
112
+ check_ruff(temp_file_path)
113
+ except subprocess.CalledProcessError as e:
114
+ complaints["ruff"] = e.output
115
+
116
+ try:
117
+ check_mypy(temp_file_path)
118
+ except subprocess.CalledProcessError as e:
119
+ complaints["mypy"] = e.output
120
+
121
+ # raise ValueError if ruff or mypy had complaints
122
+ if any(complaints.values()):
123
+ code_str = f"```{temp_file_path}\n{v}```"
124
+ error_messages = [
125
+ f"```{key}\n{value}```"
126
+ for key, value in complaints.items()
127
+ if value
128
+ ]
129
+ raise ValueError("\n\n".join([code_str] + error_messages))
130
+
131
+ finally:
132
+ os.remove(temp_file_path)
133
+ return v
134
+
135
+
136
+ def check_code(code: str) -> str:
137
+ try:
138
+ code_obj = PythonCode(code=code)
139
+ return f"# LGTM\n# use the `submit` tool to submit this code:\n\n```python\n{code_obj.code}\n```"
140
+ except ValidationError as e:
141
+ return e.errors()[0]["msg"]
142
+
143
+
144
+ prompt = ChatPromptTemplate.from_messages(
145
+ [
146
+ (
147
+ "system",
148
+ "You are a world class Python coder who uses black, ruff, and *strict* mypy for all of your code. "
149
+ "Provide complete, end-to-end Python code to meet the user's description/requirements. "
150
+ "Always `check` your code. When you're done, you must ALWAYS use the `submit` tool.",
151
+ ),
152
+ MessagesPlaceholder(variable_name="chat_history"),
153
+ (
154
+ "human",
155
+ "{input}",
156
+ ),
157
+ ],
158
+ )
159
+
160
+ check_code_tool = Tool.from_function(
161
+ check_code,
162
+ name="check-code",
163
+ description="Always check your code before submitting it!",
164
+ )
165
+
166
+ submit_code_tool = Tool.from_function(
167
+ lambda s: strip_python_markdown_tags(s),
168
+ name="submit-code",
169
+ description="THIS TOOL is the most important. use it to submit your code to the user who requested it... but be sure to `check` it first!",
170
+ return_direct=True,
171
+ )
172
+
173
+ tools = [check_code_tool, submit_code_tool]
174
+
175
+
176
+ def get_agent(
177
+ llm: BaseLLM,
178
+ agent_type: AgentType = AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
179
+ ):
180
+ agent_memory = ConversationBufferMemory(
181
+ return_messages=True,
182
+ memory_key="chat_history",
183
+ )
184
+ return initialize_agent(
185
+ tools,
186
+ llm,
187
+ agent=agent_type,
188
+ verbose=True,
189
+ handle_parsing_errors=True,
190
+ memory=agent_memory,
191
+ prompt=prompt,
192
+ return_intermediate_steps=False,
193
+ ) | (lambda output: output["output"])
langchain-streamlit-demo/qagen.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ from langchain.output_parsers import PydanticOutputParser, OutputFixingParser
4
+ from langchain.prompts.chat import (
5
+ ChatPromptTemplate,
6
+ )
7
+ from langchain.schema.language_model import BaseLanguageModel
8
+ from langchain.schema.retriever import BaseRetriever
9
+ from langchain.schema.runnable import RunnablePassthrough, RunnableSequence
10
+ from pydantic import BaseModel, Field
11
+
12
+
13
+ class QuestionAnswerPair(BaseModel):
14
+ question: str = Field(..., description="The question that will be answered.")
15
+ answer: str = Field(..., description="The answer to the question that was asked.")
16
+
17
+ def to_str(self, idx: int) -> str:
18
+ question_piece = f"{idx}. **Q:** {self.question}"
19
+ whitespace = " " * (len(str(idx)) + 2)
20
+ answer_piece = f"{whitespace}**A:** {self.answer}"
21
+ return f"{question_piece}\n\n{answer_piece}"
22
+
23
+
24
+ class QuestionAnswerPairList(BaseModel):
25
+ QuestionAnswerPairs: List[QuestionAnswerPair]
26
+
27
+ def to_str(self) -> str:
28
+ return "\n\n".join(
29
+ [
30
+ qap.to_str(idx)
31
+ for idx, qap in enumerate(self.QuestionAnswerPairs, start=1)
32
+ ],
33
+ )
34
+
35
+
36
+ PYDANTIC_PARSER: PydanticOutputParser = PydanticOutputParser(
37
+ pydantic_object=QuestionAnswerPairList,
38
+ )
39
+
40
+
41
+ templ1 = """You are a smart assistant designed to help college professors come up with reading comprehension questions.
42
+ Given a piece of text, you must come up with question and answer pairs that can be used to test a student's reading comprehension abilities.
43
+ Generate as many question/answer pairs as you can.
44
+ When coming up with the question/answer pairs, you must respond in the following format:
45
+ {format_instructions}
46
+
47
+ Do not provide additional commentary and do not wrap your response in Markdown formatting. Return RAW, VALID JSON.
48
+ """
49
+ templ2 = """{prompt}
50
+ Please create question/answer pairs, in the specified JSON format, for the following text:
51
+ ----------------
52
+ {context}"""
53
+ CHAT_PROMPT = ChatPromptTemplate.from_messages(
54
+ [
55
+ ("system", templ1),
56
+ ("human", templ2),
57
+ ],
58
+ ).partial(format_instructions=PYDANTIC_PARSER.get_format_instructions)
59
+
60
+
61
+ def get_rag_qa_gen_chain(
62
+ retriever: BaseRetriever,
63
+ llm: BaseLanguageModel,
64
+ input_key: str = "prompt",
65
+ ) -> RunnableSequence:
66
+ return (
67
+ {"context": retriever, input_key: RunnablePassthrough()}
68
+ | CHAT_PROMPT
69
+ | llm
70
+ | OutputFixingParser.from_llm(llm=llm, parser=PYDANTIC_PARSER)
71
+ | (lambda parsed_output: parsed_output.to_str())
72
+ )
langchain-streamlit-demo/research_assistant/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from research_assistant.chain import get_chain
2
+
3
+ __all__ = ["get_chain"]
langchain-streamlit-demo/research_assistant/chain.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.pydantic_v1 import BaseModel
2
+ from langchain_core.runnables import RunnablePassthrough
3
+
4
+ from research_assistant.search.web import get_search_chain
5
+ from research_assistant.writer import get_writer_chain
6
+ from langchain.llms.base import BaseLLM
7
+ from langchain.schema.runnable import Runnable
8
+
9
+
10
+ def get_chain(search_llm: BaseLLM, writer_llm: BaseLLM) -> Runnable:
11
+ chain_notypes = RunnablePassthrough().assign(
12
+ research_summary=get_search_chain(search_llm),
13
+ ) | get_writer_chain(writer_llm)
14
+
15
+ class InputType(BaseModel):
16
+ question: str
17
+
18
+ return chain_notypes.with_types(input_type=InputType)
langchain-streamlit-demo/research_assistant/search/__init__.py ADDED
File without changes
langchain-streamlit-demo/research_assistant/search/web.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from typing import Any
3
+
4
+ import requests
5
+ from bs4 import BeautifulSoup
6
+ from langchain.llms.base import BaseLLM
7
+ from langchain.prompts import ChatPromptTemplate
8
+ from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever
9
+ from langchain.utilities import DuckDuckGoSearchAPIWrapper
10
+ from langchain_core.messages import SystemMessage
11
+ from langchain_core.output_parsers import StrOutputParser
12
+ from langchain_core.runnables import (
13
+ ConfigurableField,
14
+ Runnable,
15
+ RunnableLambda,
16
+ RunnableParallel,
17
+ RunnablePassthrough,
18
+ )
19
+
20
+ RESULTS_PER_QUESTION = 3
21
+
22
+ ddg_search = DuckDuckGoSearchAPIWrapper()
23
+
24
+
25
+ def scrape_text(url: str):
26
+ # Send a GET request to the webpage
27
+ try:
28
+ response = requests.get(url)
29
+
30
+ # Check if the request was successful
31
+ if response.status_code == 200:
32
+ # Parse the content of the request with BeautifulSoup
33
+ soup = BeautifulSoup(response.text, "html.parser")
34
+
35
+ # Extract all text from the webpage
36
+ page_text = soup.get_text(separator=" ", strip=True)
37
+
38
+ # Print the extracted text
39
+ return page_text
40
+ else:
41
+ return f"Failed to retrieve the webpage: Status code {response.status_code}"
42
+ except Exception as e:
43
+ print(e)
44
+ return f"Failed to retrieve the webpage: {e}"
45
+
46
+
47
+ def web_search(query: str, num_results: int):
48
+ results = ddg_search.results(query, num_results)
49
+ return [r["link"] for r in results]
50
+
51
+
52
+ get_links: Runnable[Any, Any] = (
53
+ RunnablePassthrough()
54
+ | RunnableLambda(
55
+ lambda x: [
56
+ {"url": url, "question": x["question"]}
57
+ for url in web_search(query=x["question"], num_results=RESULTS_PER_QUESTION)
58
+ ],
59
+ )
60
+ ).configurable_alternatives(
61
+ ConfigurableField("search_engine"),
62
+ default_key="duckduckgo",
63
+ tavily=RunnableLambda(lambda x: x["question"])
64
+ | RunnableParallel(
65
+ {
66
+ "question": RunnablePassthrough(),
67
+ "results": TavilySearchAPIRetriever(k=RESULTS_PER_QUESTION),
68
+ },
69
+ )
70
+ | RunnableLambda(
71
+ lambda x: [
72
+ {"url": result.metadata["source"], "question": x["question"]}
73
+ for result in x["results"]
74
+ ],
75
+ ),
76
+ )
77
+
78
+
79
+ SEARCH_PROMPT = ChatPromptTemplate.from_messages(
80
+ [
81
+ ("system", "{agent_prompt}"),
82
+ (
83
+ "user",
84
+ "Write 3 google search queries to search online that form an "
85
+ "objective opinion from the following: {question}\n"
86
+ "You must respond with a list of strings in the following format: "
87
+ '["query 1", "query 2", "query 3"].',
88
+ ),
89
+ ],
90
+ )
91
+
92
+ AUTO_AGENT_INSTRUCTIONS = """
93
+ This task involves researching a given topic, regardless of its complexity or the availability of a definitive answer. The research is conducted by a specific agent, defined by its type and role, with each agent requiring distinct instructions.
94
+ Agent
95
+ The agent is determined by the field of the topic and the specific name of the agent that could be utilized to research the topic provided. Agents are categorized by their area of expertise, and each agent type is associated with a corresponding emoji.
96
+
97
+ examples:
98
+ task: "should I invest in apple stocks?"
99
+ response:
100
+ {
101
+ "agent": "💰 Finance Agent",
102
+ "agent_role_prompt: "You are a seasoned finance analyst AI assistant. Your primary goal is to compose comprehensive, astute, impartial, and methodically arranged financial reports based on provided data and trends."
103
+ }
104
+ task: "could reselling sneakers become profitable?"
105
+ response:
106
+ {
107
+ "agent": "📈 Business Analyst Agent",
108
+ "agent_role_prompt": "You are an experienced AI business analyst assistant. Your main objective is to produce comprehensive, insightful, impartial, and systematically structured business reports based on provided business data, market trends, and strategic analysis."
109
+ }
110
+ task: "what are the most interesting sites in Tel Aviv?"
111
+ response:
112
+ {
113
+ "agent: "🌍 Travel Agent",
114
+ "agent_role_prompt": "You are a world-travelled AI tour guide assistant. Your main purpose is to draft engaging, insightful, unbiased, and well-structured travel reports on given locations, including history, attractions, and cultural insights."
115
+ }
116
+ """ # noqa: E501
117
+ CHOOSE_AGENT_PROMPT = ChatPromptTemplate.from_messages(
118
+ [SystemMessage(content=AUTO_AGENT_INSTRUCTIONS), ("user", "task: {task}")],
119
+ )
120
+
121
+ SUMMARY_TEMPLATE = """{text}
122
+
123
+ -----------
124
+
125
+ Using the above text, answer in short the following question:
126
+
127
+ > {question}
128
+
129
+ -----------
130
+ if the question cannot be answered using the text, imply summarize the text. Include all factual information, numbers, stats etc if available.""" # noqa: E501
131
+ SUMMARY_PROMPT = ChatPromptTemplate.from_template(SUMMARY_TEMPLATE)
132
+
133
+
134
+ def load_json(s):
135
+ try:
136
+ return json.loads(s)
137
+ except Exception:
138
+ return {}
139
+
140
+
141
+ def get_search_chain(model: BaseLLM) -> Runnable:
142
+ scrape_and_summarize: Runnable[Any, Any] = (
143
+ RunnableParallel(
144
+ {
145
+ "question": lambda x: x["question"],
146
+ "text": lambda x: scrape_text(x["url"])[:10000],
147
+ "url": lambda x: x["url"],
148
+ },
149
+ )
150
+ | RunnableParallel(
151
+ {
152
+ "summary": SUMMARY_PROMPT | model | StrOutputParser(),
153
+ "url": lambda x: x["url"],
154
+ },
155
+ )
156
+ | RunnableLambda(lambda x: f"Source Url: {x['url']}\nSummary: {x['summary']}")
157
+ )
158
+
159
+ multi_search = get_links | scrape_and_summarize.map() | (lambda x: "\n".join(x))
160
+
161
+ search_query = SEARCH_PROMPT | model | StrOutputParser() | load_json
162
+ choose_agent = CHOOSE_AGENT_PROMPT | model | StrOutputParser() | load_json
163
+
164
+ get_search_queries = (
165
+ RunnablePassthrough().assign(
166
+ agent_prompt=RunnableParallel({"task": lambda x: x})
167
+ | choose_agent
168
+ | (lambda x: x.get("agent_role_prompt")),
169
+ )
170
+ | search_query
171
+ )
172
+
173
+ return (
174
+ get_search_queries
175
+ | (lambda x: [{"question": q} for q in x])
176
+ | multi_search.map()
177
+ | (lambda x: "\n\n".join(x))
178
+ )
langchain-streamlit-demo/research_assistant/writer.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts import ChatPromptTemplate
2
+ from langchain_core.output_parsers import StrOutputParser
3
+ from langchain_core.runnables import ConfigurableField
4
+ from langchain.llms.base import BaseLLM
5
+ from langchain.schema.runnable import Runnable
6
+
7
+ WRITER_SYSTEM_PROMPT = "You are an AI critical thinker research assistant. Your sole purpose is to write well written, critically acclaimed, objective and structured reports on given text." # noqa: E501
8
+
9
+
10
+ # Report prompts from https://github.com/assafelovic/gpt-researcher/blob/master/gpt_researcher/master/prompts.py
11
+ RESEARCH_REPORT_TEMPLATE = """Information:
12
+ --------
13
+ {research_summary}
14
+ --------
15
+
16
+ Using the above information, answer the following question or topic: "{question}" in a detailed report -- \
17
+ The report should focus on the answer to the question, should be well structured, informative, \
18
+ in depth, with facts and numbers if available and a minimum of 1,200 words.
19
+
20
+ You should strive to write the report as long as you can using all relevant and necessary information provided.
21
+ You must write the report with markdown syntax.
22
+ You MUST determine your own concrete and valid opinion based on the given information. Do NOT deter to general and meaningless conclusions.
23
+ Write all used source urls at the end of the report, and make sure to not add duplicated sources, but only one reference for each.
24
+ You must write the report in apa format.
25
+ Please do your best, this is very important to my career.""" # noqa: E501
26
+
27
+
28
+ RESOURCE_REPORT_TEMPLATE = """Information:
29
+ --------
30
+ {research_summary}
31
+ --------
32
+
33
+ Based on the above information, generate a bibliography recommendation report for the following question or topic: "{question}". \
34
+ The report should provide a detailed analysis of each recommended resource, explaining how each source can contribute to finding answers to the research question. \
35
+ Focus on the relevance, reliability, and significance of each source. \
36
+ Ensure that the report is well-structured, informative, in-depth, and follows Markdown syntax. \
37
+ Include relevant facts, figures, and numbers whenever available. \
38
+ The report should have a minimum length of 1,200 words.
39
+
40
+ Please do your best, this is very important to my career.""" # noqa: E501
41
+
42
+ OUTLINE_REPORT_TEMPLATE = """Information:
43
+ --------
44
+ {research_summary}
45
+ --------
46
+
47
+ Using the above information, generate an outline for a research report in Markdown syntax for the following question or topic: "{question}". \
48
+ The outline should provide a well-structured framework for the research report, including the main sections, subsections, and key points to be covered. \
49
+ The research report should be detailed, informative, in-depth, and a minimum of 1,200 words. \
50
+ Use appropriate Markdown syntax to format the outline and ensure readability.
51
+
52
+ Please do your best, this is very important to my career.""" # noqa: E501
53
+
54
+ prompt = ChatPromptTemplate.from_messages(
55
+ [
56
+ ("system", WRITER_SYSTEM_PROMPT),
57
+ ("user", RESEARCH_REPORT_TEMPLATE),
58
+ ],
59
+ ).configurable_alternatives(
60
+ ConfigurableField("report_type"),
61
+ default_key="research_report",
62
+ resource_report=ChatPromptTemplate.from_messages(
63
+ [
64
+ ("system", WRITER_SYSTEM_PROMPT),
65
+ ("user", RESOURCE_REPORT_TEMPLATE),
66
+ ],
67
+ ),
68
+ outline_report=ChatPromptTemplate.from_messages(
69
+ [
70
+ ("system", WRITER_SYSTEM_PROMPT),
71
+ ("user", OUTLINE_REPORT_TEMPLATE),
72
+ ],
73
+ ),
74
+ )
75
+
76
+
77
+ def get_writer_chain(model: BaseLLM) -> Runnable:
78
+ return prompt | model | StrOutputParser()
langchain-streamlit-demo/summarize.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chains.base import Chain
2
+ from langchain.chains.summarize import load_summarize_chain
3
+ from langchain.prompts import PromptTemplate
4
+ from langchain.schema.language_model import BaseLanguageModel
5
+ from langchain.schema.retriever import BaseRetriever
6
+ from langchain.schema.runnable import RunnableSequence, RunnablePassthrough
7
+
8
+ prompt_template = """Write a concise summary of the following text, based on the user input.
9
+ User input: {query}
10
+ Text:
11
+ ```
12
+ {text}
13
+ ```
14
+ CONCISE SUMMARY:"""
15
+
16
+ refine_template = (
17
+ "You are iteratively crafting a summary of the text below based on the user input\n"
18
+ "User input: {query}\n"
19
+ "We have provided an existing summary up to a certain point: {existing_answer}\n"
20
+ "We have the opportunity to refine the existing summary"
21
+ "(only if needed) with some more context below.\n"
22
+ "------------\n"
23
+ "{text}\n"
24
+ "------------\n"
25
+ "Given the new context, refine the original summary.\n"
26
+ "If the context isn't useful, return the original summary.\n"
27
+ "If the context is useful, refine the summary to include the new context.\n"
28
+ "Your contribution is helping to build a comprehensive summary of a large body of knowledge.\n"
29
+ "You do not have the complete context, so do not discard pieces of the original summary."
30
+ )
31
+
32
+
33
+ def get_summarization_chain(
34
+ llm: BaseLanguageModel,
35
+ prompt: str,
36
+ ) -> Chain:
37
+ _prompt = PromptTemplate.from_template(
38
+ prompt_template,
39
+ partial_variables={"query": prompt},
40
+ )
41
+ refine_prompt = PromptTemplate.from_template(
42
+ refine_template,
43
+ partial_variables={"query": prompt},
44
+ )
45
+ return load_summarize_chain(
46
+ llm=llm,
47
+ chain_type="refine",
48
+ question_prompt=_prompt,
49
+ refine_prompt=refine_prompt,
50
+ return_intermediate_steps=False,
51
+ input_key="input_documents",
52
+ output_key="output_text",
53
+ )
54
+
55
+
56
+ def get_rag_summarization_chain(
57
+ prompt: str,
58
+ retriever: BaseRetriever,
59
+ llm: BaseLanguageModel,
60
+ input_key: str = "prompt",
61
+ ) -> RunnableSequence:
62
+ return (
63
+ {"input_documents": retriever, input_key: RunnablePassthrough()}
64
+ | get_summarization_chain(llm, prompt)
65
+ | (lambda output: output["output_text"])
66
+ )
requirements.txt ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ anthropic==0.12.0
2
+ beautifulsoup4==4.12.3
3
+ black==24.1.1
4
+ certifi>=2023.7.22 # not directly required, pinned by Snyk to avoid a vulnerability
5
+ duckduckgo-search==4.3.2
6
+ faiss-cpu==1.7.4
7
+ langchain==0.1.4
8
+ langsmith==0.0.84
9
+ mypy==1.8.0
10
+ numexpr==2.9.0
11
+ numpy>=1.22.2 # not directly required, pinned by Snyk to avoid a vulnerability
12
+ openai==1.10.0
13
+ pillow>=10.2.0 # not directly required, pinned by Snyk to avoid a vulnerability
14
+ pyarrow>=14.0.1 # not directly required, pinned by Snyk to avoid a vulnerability
15
+ pypdf==4.0.1
16
+ requests>=2.31.0 # not directly required, pinned by Snyk to avoid a vulnerability
17
+ ruff==0.1.14
18
+ streamlit==1.30.0
19
+ streamlit-feedback==0.1.3
20
+ tiktoken==0.5.2
21
+ tornado>=6.3.3 # not directly required, pinned by Snyk to avoid a vulnerability
22
+ validators>=0.21.0 # not directly required, pinned by Snyk to avoid a vulnerability
23
+ wikipedia==1.4.0