Spaces:
Running
Running
Updated the article
Browse files- Articles_Quality_Check.html +72 -1
Articles_Quality_Check.html
CHANGED
|
@@ -154,7 +154,78 @@
|
|
| 154 |
| (Instantly validated JSON / Typed Python Object) |
|
| 155 |
+--------------------------------------------------+
|
| 156 |
</pre>
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
</p>
|
| 159 |
</body>
|
| 160 |
</html>
|
|
|
|
| 154 |
| (Instantly validated JSON / Typed Python Object) |
|
| 155 |
+--------------------------------------------------+
|
| 156 |
</pre>
|
| 157 |
+
Let's begin building the project. Code is organized as below.<br>
|
| 158 |
+
<pre>
|
| 159 |
+
├── article_schema.py --> Pydantic Schema for the structure
|
| 160 |
+
├── audit_workflow.py
|
| 161 |
+
├── create_report.py
|
| 162 |
+
├── fetch_articles.py --> Fetches the articles from a CMS
|
| 163 |
+
</pre>
|
| 164 |
+
<pre>
|
| 165 |
+
from pydantic import BaseModel, Field
|
| 166 |
+
from typing import List, Optional
|
| 167 |
+
#consider an article with an id, title, description fields
|
| 168 |
+
class ArticleStructure(BaseModel):
|
| 169 |
+
collectionId: str = Field(description="The unique identifier of the article.")
|
| 170 |
+
title: str = Field(short_summary="Check if title is engaging and properly cased.")
|
| 171 |
+
well_formed: bool = Field(description="Overall structural integrity of the article.")
|
| 172 |
+
missing_elements: Optional[List[str]] = Field(description="List of structural elements that are missing, if any.")
|
| 173 |
+
feedback: str = Field(description="Actionable feedback for the author.")
|
| 174 |
+
</pre>
|
| 175 |
+
Test the schema, by creating some dummy data point.<br>
|
| 176 |
+
<pre>
|
| 177 |
+
dummy_article = ArticleStructure(collectionId="1",title="some_thing", well_formed="yes", missing_elements=[], feedback="something")
|
| 178 |
+
print(dummy_article)
|
| 179 |
+
</pre>
|
| 180 |
+
Now, let's get the ball rolling and fetch some articles from a CMS system. If you don't have one, you may use some fastapi to implement a dummy article repository.
|
| 181 |
+
<br> or create a static set of text articles for testing the flow.
|
| 182 |
+
<pre>
|
| 183 |
+
import requests
|
| 184 |
+
from datetime import datetime, timedelta, timezone
|
| 185 |
+
|
| 186 |
+
def fetch_recent_articles(pocketbase_url: str):
|
| 187 |
+
# Calculate time threshold (e.g., 3 hours ago)
|
| 188 |
+
time_threshold = datetime.now(timezone.utc) - timedelta(days=50)
|
| 189 |
+
time_string = time_threshold.isoformat().replace("+00:00", "Z")
|
| 190 |
+
# Query PocketBase API, giving the full path where articles reside
|
| 191 |
+
filter_query = f"updated >= '{time_string}'"
|
| 192 |
+
url = f"{pocketbase_url}/api/collections/nature_posts/records"
|
| 193 |
+
params = {"filter": filter_query}
|
| 194 |
+
response = requests.get(url, params=params)
|
| 195 |
+
response.raise_for_status()
|
| 196 |
+
return response.json().get("items", [])
|
| 197 |
+
</pre>
|
| 198 |
+
Now, let's write a test audit program, we will use a locally hosted llm using openAI like. <br>
|
| 199 |
+
<pre>
|
| 200 |
+
import json
|
| 201 |
+
from llama_index.llms.openai_like import OpenAILike
|
| 202 |
+
import article_schema as asc
|
| 203 |
+
from llama_index.core.prompts import PromptTemplate
|
| 204 |
+
|
| 205 |
+
#creating a dummy article to test it
|
| 206 |
+
custom_llm = OpenAILike(model="gemma", api_base="http://localhost:8080/v1", api_key="not_needed", is_chat_model=True)
|
| 207 |
+
|
| 208 |
+
prompt = f"""
|
| 209 |
+
Analyze the following article for well-formed structure, tone, and readability.
|
| 210 |
+
CollectionId: {'something'}
|
| 211 |
+
Title: {'Rose, a beautiful plant'}
|
| 212 |
+
Content: {'Roses are very beautiful plants and grow in different colors. They are great addition to any garden! Roses are popular woody shrubs that belong to the genus Rosa.They grow colorful flowers in shades of red, pink, yellow, and white.Their stems have sharp prickles, commonly called thorns, to protect against animals.Most rose varieties produce a sweet and pleasant fragrance.'}
|
| 213 |
+
"""
|
| 214 |
+
|
| 215 |
+
# Use structured output to force the Pydantic schema evaluation
|
| 216 |
+
response = custom_llm.structured_predict(
|
| 217 |
+
output_cls=asc.ArticleStructure,
|
| 218 |
+
prompt=PromptTemplate(prompt)
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
print(response)
|
| 222 |
+
</pre>
|
| 223 |
+
If everything goes well, you should be able to see something like this. <br>
|
| 224 |
+
<pre>
|
| 225 |
+
collectionId='CollectionId' title='Rose, a beautiful plant' well_formed=True missing_elements=[] feedback='The article is well-formed with a clear structure. It follows a simple, direct narrative. The title is engaging and appropriately formatted. The content is concise and informative, providing a basic overview of roses. There are no missing elements.'
|
| 226 |
+
</pre>
|
| 227 |
+
Expand on the concept and implement a simple workflow using llamaindex to complete the quality check of the contents of the articles.<br>
|
| 228 |
+
<b>Full code will be presented at a later point of time!!</b>
|
| 229 |
</p>
|
| 230 |
</body>
|
| 231 |
</html>
|