Add black step when testing examples (#6425)

This commit is contained in:
Silvano Cerza 2023-11-27 15:01:33 +01:00 committed by GitHub
parent 09b4f53ce5
commit db759b0717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 36 deletions

View File

@ -19,8 +19,80 @@ env:
PYTHON_VERSION: "3.8"
jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install Black
run: |
pip install --upgrade pip
pip install .[dev]
- name: Check status
run: |
if ! black . --check; then
git status
echo "###################################################################################################"
echo "# "
echo "# CHECK FAILED! Black found issues with your code formatting."
echo "# "
echo "# Either:"
echo "# 1. Run Black locally before committing:"
echo "# "
echo "# pip install .[formatting]"
echo "# black ."
echo "# "
echo "# 2. Install the pre-commit hook:"
echo "# "
echo "# pre-commit install"
echo "# "
echo "# 3. See https://github.com/deepset-ai/haystack/blob/main/CONTRIBUTING.md for help."
echo "# "
echo "# If you have further problems, please open an issue: https://github.com/deepset-ai/haystack/issues"
echo "# "
echo "##################################################################################################"
exit 1
fi
- name: Calculate alert data
id: calculator
shell: bash
if: (success() || failure()) && github.ref_name == 'main'
run: |
if [ "${{ job.status }}" = "success" ]; then
echo "alert_type=success" >> "$GITHUB_OUTPUT";
else
echo "alert_type=error" >> "$GITHUB_OUTPUT";
fi
- name: Send event to Datadog
if: (success() || failure()) && github.ref_name == 'main'
uses: masci/datadog@v1
with:
api-key: ${{ secrets.CORE_DATADOG_API_KEY }}
api-url: https://api.datadoghq.eu
events: |
- title: "${{ github.workflow }} workflow"
text: "Job ${{ github.job }} in branch ${{ github.ref_name }}"
alert_type: "${{ steps.calculator.outputs.alert_type }}"
source_type_name: "Github"
host: ${{ github.repository_owner }}
tags:
- "project:${{ github.repository }}"
- "job:${{ github.job }}"
- "run_id:${{ github.run_id }}"
- "workflow:${{ github.workflow }}"
- "branch:${{ github.ref_name }}"
- "url:https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
tests:
name: Snippets
needs: black
runs-on: ubuntu-latest
steps:

View File

@ -44,25 +44,6 @@ jobs:
run: |
if ! black . --check; then
git status
echo "###################################################################################################"
echo "# "
echo "# CHECK FAILED! Black found issues with your code formatting."
echo "# "
echo "# Either:"
echo "# 1. Run Black locally before committing:"
echo "# "
echo "# pip install .[formatting]"
echo "# black ."
echo "# "
echo "# 2. Install the pre-commit hook:"
echo "# "
echo "# pre-commit install"
echo "# "
echo "# 3. See https://github.com/deepset-ai/haystack/blob/main/CONTRIBUTING.md for help."
echo "# "
echo "# If you have further problems, please open an issue: https://github.com/deepset-ai/haystack/issues"
echo "# "
echo "##################################################################################################"
exit 1
fi

View File

@ -24,9 +24,11 @@ class City(BaseModel):
country: str
population: int
class CitiesData(BaseModel):
cities: List[City]
schema = CitiesData.schema_json(indent=2)
@ -34,18 +36,13 @@ schema = CitiesData.schema_json(indent=2)
# and validates if this is compliant with our schema.
# If not, it returns also the error message so that we have a better chance of correcting it in the next loop
@component
class OutputParser():
def __init__(self, pydantic_model:pydantic.BaseModel):
class OutputParser:
def __init__(self, pydantic_model: pydantic.BaseModel):
self.pydantic_model = pydantic_model
self.iteration_counter = 0
@component.output_types(valid=List[str],
invalid=Optional[List[str]],
error_message=Optional[str])
def run(
self,
replies: List[str]):
@component.output_types(valid=List[str], invalid=Optional[List[str]], error_message=Optional[str])
def run(self, replies: List[str]):
self.iteration_counter += 1
# let's simulate a corrupt JSON with 30% probability by adding extra brackets (for demo purposes)
@ -55,13 +52,17 @@ class OutputParser():
try:
output_dict = json.loads(replies[0])
self.pydantic_model.parse_obj(output_dict)
print(f"OutputParser at Iteration {self.iteration_counter}: Valid JSON from LLM - No need for looping: {replies[0]}")
print(
f"OutputParser at Iteration {self.iteration_counter}: Valid JSON from LLM - No need for looping: {replies[0]}"
)
return {"valid": replies}
except (ValueError, ValidationError) as e:
print(f"OutputParser at Iteration {self.iteration_counter}: Invalid JSON from LLM - Let's try again.\n"
f"Output from LLM:\n {replies[0]} \n"
f"Error from OutputParser: {e}")
print(
f"OutputParser at Iteration {self.iteration_counter}: Invalid JSON from LLM - Let's try again.\n"
f"Output from LLM:\n {replies[0]} \n"
f"Error from OutputParser: {e}"
)
return {"invalid": replies, "error_message": str(e)}
@ -92,9 +93,6 @@ pipeline.connect("output_parser.error_message", "prompt_builder.error_message")
# Now, let's run our pipeline with an example passage that we want to convert into our JSON format
passage = "Berlin is the capital of Germany. It has a population of 3,850,809"
result = pipeline.run({
"prompt_builder": {"passage": passage,
"schema": schema}
})
result = pipeline.run({"prompt_builder": {"passage": passage, "schema": schema}})
print(result)