-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from ittia-research/dev
Add JSON return format support, add PyPI package for API connect, update pipeline
- Loading branch information
Showing
9 changed files
with
277 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
.* | ||
__pycache__/ | ||
|
||
# poetry build | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
This package connects to the ITTIA Check API. | ||
|
||
More on this project and how to self-host one: https://github.com/ittia-research/check | ||
|
||
## How-to | ||
Demo on how to fact-check a text: | ||
```python | ||
import asyncio | ||
from ittia_check import Check | ||
|
||
base_url = "https://check.ittia.net" | ||
format = "json" # or markdown | ||
|
||
check = Check(base_url=base_url, format=format) | ||
|
||
query = "Germany hosted the 2024 Olympics" | ||
|
||
result = asyncio.run(check(query)) | ||
|
||
print(result) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import httpx | ||
import json | ||
import logging | ||
|
||
API_BASE_URL = "https://check.ittia.net" | ||
|
||
class Check(): | ||
""" | ||
Fact-check a string and returns verdicts in markdown or JSON formats. | ||
""" | ||
def __init__(self, | ||
base_url: str = API_BASE_URL, | ||
format: str = 'markdown', | ||
timeout: int = 600, | ||
): | ||
""" | ||
Args: | ||
- base_url: API base URL | ||
- format: markdown | json, return format | ||
""" | ||
self.base_url = base_url | ||
self.format = format | ||
self.timeout = timeout # api request timeout, set higher cause the process might take a long time | ||
|
||
self.headers = { | ||
"X-Return-Format": self.format, | ||
"Accept": "text/event-stream", | ||
} | ||
self.client = httpx.AsyncClient(follow_redirects=True, timeout=self.timeout) | ||
|
||
async def __call__(self, query: str): | ||
""" | ||
Args: | ||
- query: text to check | ||
""" | ||
url = self.base_url + '/' + query | ||
result = None | ||
|
||
async with self.client.stream("GET", url, headers=self.headers) as response: | ||
buffer = "" | ||
async for chunk in response.aiter_text(): | ||
if chunk.strip(): # Only process non-empty chunks | ||
buffer += chunk | ||
|
||
# Attempt to load the buffer as JSON | ||
try: | ||
# Keep loading JSON until all data is consumed | ||
while buffer: | ||
# Try to load a complete JSON object | ||
rep, index = json.JSONDecoder().raw_decode(buffer) | ||
|
||
# Select the `final` stage only | ||
if rep['stage'] != 'final': | ||
logging.debug(f"Stage {rep['stage']}: {rep['content']}") | ||
else: | ||
result = rep['content'] | ||
|
||
# Remove processed JSON and any leading whitespace from the buffer | ||
buffer = buffer[index:].lstrip() | ||
except json.JSONDecodeError: | ||
# If we encounter an error, we may not have a complete JSON object yet | ||
continue # Continue to read more data | ||
|
||
if not result: | ||
logging.warning("No result found") | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[tool.poetry] | ||
name = "ittia-check" | ||
version = "0.1.0" | ||
description = "Connect to the ITTIA Check API or self-hosted ones" | ||
authors = ["ITTIA <[email protected]>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
homepage = "https://github.com/ittia-research/check" | ||
repository = "https://github.com/ittia-research/check" | ||
keywords = ["fact-check", "ai", "llm", "rag"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.8" | ||
httpx = "^0.27.2" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.