-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
910 additions
and
30 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
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
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
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,26 @@ | ||
from flask import Blueprint, render_template, request | ||
from flask.typing import ResponseReturnValue | ||
|
||
from starter.query.query_service import QueryService | ||
|
||
|
||
def index_page(query_service: QueryService) -> Blueprint: | ||
page = Blueprint('index_page', __name__) | ||
|
||
@page.get('/') | ||
def index() -> ResponseReturnValue: | ||
return render_template('index.html') | ||
|
||
@page.post('/') | ||
def query() -> ResponseReturnValue: | ||
user_query = request.form.get('query') | ||
result = query_service.fetch_response(user_query) | ||
|
||
return render_template( | ||
'response.html', | ||
query=user_query, | ||
source=result.source, | ||
response=result.response, | ||
) | ||
|
||
return page |
Empty file.
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,31 @@ | ||
from dataclasses import dataclass | ||
|
||
from starter.ai.open_ai_client import OpenAIClient, ChatMessage | ||
from starter.search.chunks_search_service import ChunksSearchService | ||
|
||
|
||
@dataclass | ||
class QueryResult: | ||
source: str | ||
response: str | ||
|
||
|
||
class QueryService: | ||
def __init__(self, chunks_search_service: ChunksSearchService, ai_client: OpenAIClient): | ||
self.chunks_search_service = chunks_search_service | ||
self.ai_client = ai_client | ||
|
||
def fetch_response(self, query: str) -> QueryResult: | ||
chunk = self.chunks_search_service.search_for_relevant_chunk(query) | ||
response = self.ai_client.fetch_chat_completion([ | ||
ChatMessage(role="system", content="You are a reporter for a major world newspaper."), | ||
ChatMessage(role="system", content="Write your response as if you were writing a short, high-quality news" | ||
"article for your paper. Limit your response to one paragraph."), | ||
ChatMessage(role="system", content=f"Use the following article for context: {chunk.content}"), | ||
ChatMessage(role="user", content=query), | ||
]) | ||
|
||
return QueryResult( | ||
source=chunk.source, | ||
response=response | ||
) |
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,35 @@ | ||
from dataclasses import dataclass | ||
|
||
from starter.ai.open_ai_client import OpenAIClient | ||
from starter.documents.chunks_gateway import ChunksGateway | ||
from starter.documents.documents_gateway import DocumentsGateway | ||
from starter.search.embeddings_gateway import EmbeddingsGateway | ||
|
||
|
||
@dataclass | ||
class ChunkSearchResult: | ||
content: str | ||
source: str | ||
|
||
|
||
class ChunksSearchService: | ||
def __init__(self, | ||
embeddings_gateway: EmbeddingsGateway, | ||
chunks_gateway: ChunksGateway, | ||
documents_gateway: DocumentsGateway, | ||
open_ai_client: OpenAIClient): | ||
self.embeddings_gateway = embeddings_gateway | ||
self.chunks_gateway = chunks_gateway | ||
self.documents_gateway = documents_gateway | ||
self.open_ai_client = open_ai_client | ||
|
||
def search_for_relevant_chunk(self, query: str) -> ChunkSearchResult: | ||
vector = self.open_ai_client.fetch_embedding(query) | ||
chunk_id = self.embeddings_gateway.find_similar_chunk_id(vector) | ||
chunk = self.chunks_gateway.find(chunk_id) | ||
document = self.documents_gateway.find(chunk.document_id) | ||
|
||
return ChunkSearchResult( | ||
content=chunk.content, | ||
source=document.source, | ||
) |
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,5 @@ | ||
from typing import List | ||
|
||
|
||
def vector_to_string(vector: List[float]) -> str: | ||
return "[" + ",".join([str(v) for v in vector]) + "]" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.