examples.api_example
Using GitHubSearchEngine in an API.
First, build a simple API using FastAPI. The API handles indexing a repo, and searching through an indexed repo.
1import os
2from contextlib import asynccontextmanager
3from typing import Optional
4
5from fastapi import FastAPI
6from pydantic import BaseModel
7
8from github_search_engine.github_search_engine import GithubSearchEngine
9
10
11search_engine: Optional[GithubSearchEngine] = None
12
13
14class Repository(BaseModel):
15 owner: str
16 repository_name: str
17
18
19@asynccontextmanager
20async def lifespan(app: FastAPI):
21 global search_engine
22 github_access_token = os.environ["GITHUB_PAT"]
23 search_engine = GithubSearchEngine(
24 github_access_token, qdrant_location=":memory:"
25 )
26 yield
27 del search_engine
28
29
30api = FastAPI(lifespan=lifespan)
31
32
33@api.post("/index")
34async def index(repository: Repository):
35 await search_engine.index_repository(
36 repository.owner,
37 repository.repository_name,
38 )
39 return {"message": "Repository indexed"}
40
41
42@api.get("/search")
43async def search(owner: str, repository: str, query: str):
44 results = search_engine.search(owner, repository, query)
45 summary = search_engine.summarise_results(results, owner, repository, query)
46 return summary
Then, we can launch the API with uvicorn.
1import uvicorn
2
3
4if __name__ == "__main__":
5 uvicorn.run("github_search_engine._api.api:api", host="0.0.0.0", port=8000)