Skip to content

Commit

Permalink
Merge branch 'main' into ross/647
Browse files Browse the repository at this point in the history
  • Loading branch information
ross-p-smith authored May 17, 2024
2 parents 96ec37f + 0dedf7f commit 1995d87
Show file tree
Hide file tree
Showing 14 changed files with 602 additions and 159 deletions.
15 changes: 15 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
- [ ] Yes
- [ ] No

<!-- Please prefix your PR title with one of the following:
* `feat`: A new feature
* `fix`: A bug fix
* `docs`: Documentation only changes
* `style`: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
* `refactor`: A code change that neither fixes a bug nor adds a feature
* `perf`: A code change that improves performance
* `test`: Adding missing tests or correcting existing tests
* `build`: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
* `ci`: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
* `chore`: Other changes that don't modify src or test files
* `revert`: Reverts a previous commit
* !: A breaking change is indicated with a `!` after the listed prefixes above, e.g. `feat!`, `fix!`, `refactor!`, etc.
-->

## How to Test
* Get the code

Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
workflow_run:
workflows: ["CI"]
types:
- completed

permissions:
contents: write

name: create-release

jobs:
create-release:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Conventional Changelog Action
id: changelog
uses: TriPSs/conventional-changelog-action@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
output-file: 'CHANGELOG.md'
git-user-name: 'github-actions[bot]'
git-user-email: 'github-actions[bot]@users.noreply.github.com'
version-file: './pyproject.toml'
version-path: 'tool.poetry.version'


- name: Create Release
uses: actions/create-release@v1
if: ${{ steps.changelog.outputs.skipped == 'false' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.changelog.outputs.tag }}
release_name: ${{ steps.changelog.outputs.tag }}
body: ${{ steps.changelog.outputs.clean_changelog }}
23 changes: 0 additions & 23 deletions .github/workflows/release-please.yml

This file was deleted.

12 changes: 12 additions & 0 deletions code/backend/batch/utilities/common/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def __init__(
self.prompt_tokens = prompt_tokens
self.completion_tokens = completion_tokens

def __eq__(self, value: object) -> bool:
if not isinstance(value, Answer):
return False

return (
self.question == value.question
and self.answer == value.answer
and self.source_documents == value.source_documents
and self.prompt_tokens == value.prompt_tokens
and self.completion_tokens == value.completion_tokens
)

def to_json(self):
return json.dumps(self, cls=AnswerEncoder)

Expand Down
48 changes: 19 additions & 29 deletions code/backend/batch/utilities/tools/post_prompt_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.callbacks import get_openai_callback
from ..common.answer import Answer
from ..helpers.llm_helper import LLMHelper
from ..helpers.config.config_helper import ConfigHelper
Expand All @@ -14,53 +11,46 @@ def validate_answer(self, answer: Answer) -> Answer:
config = ConfigHelper.get_active_config_or_default()
llm_helper = LLMHelper()

was_message_filtered = False
post_answering_prompt = PromptTemplate(
template=config.prompts.post_answering_prompt,
input_variables=["question", "answer", "sources"],
)
post_answering_chain = LLMChain(
llm=llm_helper.get_llm(),
prompt=post_answering_prompt,
output_key="correct",
verbose=True,
)

sources = "\n".join(
[
f"[doc{i+1}]: {source.content}"
for i, source in enumerate(answer.source_documents)
]
)

with get_openai_callback() as cb:
post_result = post_answering_chain(
message = config.prompts.post_answering_prompt.format(
question=answer.question,
answer=answer.answer,
sources=sources,
)

response = llm_helper.get_chat_completion(
[
{
"question": answer.question,
"answer": answer.answer,
"sources": sources,
"role": "user",
"content": message,
}
)

was_message_filtered = not (
post_result["correct"].lower() == "true"
or post_result["correct"].lower() == "yes"
]
)

result = response.choices[0].message.content

was_message_filtered = result.lower() not in ["true", "yes"]

# Return filtered answer or just the original one
if was_message_filtered:
return Answer(
question=answer.question,
answer=config.messages.post_answering_filter,
source_documents=[],
prompt_tokens=cb.prompt_tokens,
completion_tokens=cb.completion_tokens,
prompt_tokens=response.usage.prompt_tokens,
completion_tokens=response.usage.completion_tokens,
)
else:
return Answer(
question=answer.question,
answer=answer.answer,
source_documents=answer.source_documents,
prompt_tokens=cb.prompt_tokens,
completion_tokens=cb.completion_tokens,
prompt_tokens=response.usage.prompt_tokens,
completion_tokens=response.usage.completion_tokens,
)
164 changes: 84 additions & 80 deletions code/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,86 +16,6 @@ def setup_default_mocking(httpserver: HTTPServer, app_config: AppConfig):
method="HEAD",
).respond_with_data()

httpserver.expect_request(
f"/{AZURE_STORAGE_CONFIG_CONTAINER_NAME}/{AZURE_STORAGE_CONFIG_FILE_NAME}",
method="GET",
).respond_with_json(
{
"prompts": {
"condense_question_prompt": "",
"answering_system_prompt": "system prompt",
"answering_user_prompt": "## Retrieved Documents\n{sources}\n\n## User Question\n{question}",
"use_on_your_data_format": True,
"post_answering_prompt": "post answering prompt",
"enable_post_answering_prompt": False,
"enable_content_safety": True,
},
"messages": {"post_answering_filter": "post answering filer"},
"example": {
"documents": '{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}',
"user_question": "user question",
"answer": "answer",
},
"document_processors": [
{
"document_type": "pdf",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "layout"},
"use_advanced_image_processing": False,
},
{
"document_type": "txt",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "url",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "md",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "html",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "docx",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "docx"},
"use_advanced_image_processing": False,
},
{
"document_type": "jpg",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "layout"},
"use_advanced_image_processing": True,
},
{
"document_type": "png",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "layout"},
"use_advanced_image_processing": False,
},
],
"logging": {"log_user_interactions": True, "log_tokens": True},
"orchestrator": {"strategy": "openai_function"},
"integrated_vectorization_config": None,
},
headers={
"Content-Type": "application/json",
"Content-Range": "bytes 0-12882/12883",
},
)

httpserver.expect_request(
f"/openai/deployments/{app_config.get('AZURE_OPENAI_EMBEDDING_MODEL')}/embeddings",
method="POST",
Expand Down Expand Up @@ -269,3 +189,87 @@ def prime_search_to_trigger_creation_of_index(
"/indexes",
method="GET",
).respond_with_json({"value": [{"name": app_config.get("AZURE_SEARCH_INDEX")}]})


# This fixture can be overriden
@pytest.fixture(autouse=True)
def setup_config_mocking(httpserver: HTTPServer):
httpserver.expect_request(
f"/{AZURE_STORAGE_CONFIG_CONTAINER_NAME}/{AZURE_STORAGE_CONFIG_FILE_NAME}",
method="GET",
).respond_with_json(
{
"prompts": {
"condense_question_prompt": "",
"answering_system_prompt": "system prompt",
"answering_user_prompt": "## Retrieved Documents\n{sources}\n\n## User Question\n{question}",
"use_on_your_data_format": True,
"post_answering_prompt": "post answering prompt\n{question}\n{answer}\n{sources}",
"enable_post_answering_prompt": False,
"enable_content_safety": True,
},
"messages": {"post_answering_filter": "post answering filter"},
"example": {
"documents": '{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}',
"user_question": "user question",
"answer": "answer",
},
"document_processors": [
{
"document_type": "pdf",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "layout"},
"use_advanced_image_processing": False,
},
{
"document_type": "txt",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "url",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "md",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "html",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
"use_advanced_image_processing": False,
},
{
"document_type": "docx",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "docx"},
"use_advanced_image_processing": False,
},
{
"document_type": "jpg",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "layout"},
"use_advanced_image_processing": True,
},
{
"document_type": "png",
"chunking": {"strategy": "layout", "size": 500, "overlap": 100},
"loading": {"strategy": "layout"},
"use_advanced_image_processing": False,
},
],
"logging": {"log_user_interactions": True, "log_tokens": True},
"orchestrator": {"strategy": "openai_function"},
"integrated_vectorization_config": None,
},
headers={
"Content-Type": "application/json",
"Content-Range": "bytes 0-12882/12883",
},
)
Loading

0 comments on commit 1995d87

Please sign in to comment.