Refactor person search and bio generation to accept author ID and query parameters
This commit is contained in:
@@ -59,16 +59,26 @@ def insert_element(db: Session, value: S) -> S:
|
||||
db.refresh(value)
|
||||
return value
|
||||
|
||||
def search_persons() -> model.PersonSearchResult:
|
||||
def search_persons(author_id: Optional[str] = None, query: Optional[str] = None) -> model.PersonSearchResult:
|
||||
headers = {
|
||||
"Authorization": f"ApiKey {config.settings.elasticsearch_api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
if author_id:
|
||||
query_field = "_id"
|
||||
query_value = author_id
|
||||
elif query:
|
||||
query_field = "display_name"
|
||||
query_value = query
|
||||
else:
|
||||
return "Either author_id or query must be provided"
|
||||
|
||||
payload = {
|
||||
"query": {
|
||||
"query_string": {
|
||||
"default_field": "display_name",
|
||||
"query": "Arman AND Arzani"
|
||||
"default_field": query_field,
|
||||
"query": query_value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,8 +113,8 @@ def search_persons() -> model.PersonSearchResult:
|
||||
|
||||
return model.PersonSearchResult(persons=persons)
|
||||
|
||||
def generate_person_bio() -> str:
|
||||
search_result = search_persons()
|
||||
def generate_person_bio(author_id: str) -> str:
|
||||
search_result = search_persons(author_id=author_id)
|
||||
if not search_result.persons:
|
||||
return "No person found."
|
||||
if len(search_result.persons) > 1:
|
||||
|
||||
@@ -10,6 +10,7 @@ from pydantic import Field
|
||||
from typing import List
|
||||
from typing import TypeVar
|
||||
from typing import Generic
|
||||
from typing import Optional
|
||||
|
||||
|
||||
T = TypeVar('T', bound=BaseModel)
|
||||
@@ -40,7 +41,7 @@ class PersonBio(BaseModel):
|
||||
|
||||
class Affiliation(BaseModel):
|
||||
institution: str = Field(description="Institution name.")
|
||||
country_code: str = Field(description="Country code.")
|
||||
country_code: Optional[str] = Field(default=None, description="Country code.")
|
||||
|
||||
class Person(BaseModel):
|
||||
id: str = Field(description="The person id.")
|
||||
|
||||
@@ -116,16 +116,17 @@ def delete_item(id: Annotated[int, Path(description='The item id.', ge=0)],
|
||||
@app.get('/persons/search',
|
||||
operation_id='searchPersons',
|
||||
response_model=model.PersonSearchResult,
|
||||
summary='Searches for persons.',
|
||||
summary='Searches for persons by ID or name.',
|
||||
tags=[ 'Persons' ])
|
||||
def search_persons():
|
||||
return logic.search_persons()
|
||||
def search_persons(author_id: Annotated[str, Query(description="OpenAlex author ID (e.g., A5093075883)")] = None,
|
||||
query: Annotated[str, Query(description="Search query (e.g., Arman AND Arzani)")] = None):
|
||||
return logic.search_persons(author_id, query)
|
||||
|
||||
@app.get('/persons/bio',
|
||||
operation_id='generatePersonBio',
|
||||
response_model=model.PersonBio,
|
||||
summary='Generates a bio for a person.',
|
||||
summary='Generates a bio for a person by author ID.',
|
||||
tags=[ 'Persons' ])
|
||||
def generate_person_bio():
|
||||
bio = logic.generate_person_bio()
|
||||
def generate_person_bio(author_id: Annotated[str, Query(description="OpenAlex author ID (e.g., A5093075883)")]):
|
||||
bio = logic.generate_person_bio(author_id)
|
||||
return model.PersonBio(bio=bio)
|
||||
|
||||
Reference in New Issue
Block a user