It works for the first template. V0.1

This commit is contained in:
Guillem Borrell 2024-05-25 16:09:20 +02:00
parent f210b811af
commit 8282f8e0dd
7 changed files with 18 additions and 14 deletions

View file

@ -6,7 +6,7 @@ from pydantic import BaseModel
import hellocomputer import hellocomputer
from .routers import files, sessions from .routers import files, sessions, analysis
static_path = Path(hellocomputer.__file__).parent / "static" static_path = Path(hellocomputer.__file__).parent / "static"
@ -42,6 +42,7 @@ def get_health() -> HealthCheck:
app.include_router(sessions.router) app.include_router(sessions.router)
app.include_router(files.router) app.include_router(files.router)
app.include_router(analysis.router)
app.mount( app.mount(
"/", "/",
StaticFiles(directory=static_path, html=True, packages=["bootstrap4"]), StaticFiles(directory=static_path, html=True, packages=["bootstrap4"]),

View file

@ -14,12 +14,13 @@ router = APIRouter()
@router.get("/query", response_class=PlainTextResponse, tags=["queries"]) @router.get("/query", response_class=PlainTextResponse, tags=["queries"])
async def query(sid: str = "", q: str = "") -> str: async def query(sid: str = "", q: str = "") -> str:
print(q)
query = f"Write a query that {q} in the current database" query = f"Write a query that {q} in the current database"
chat = Chat(api_key=settings.anyscale_api_key, temperature=0.5) chat = Chat(api_key=settings.anyscale_api_key, temperature=0.5)
db = ( db = (
DDB() DDB()
.gcs_secret(settings.gcs_secret, settings.gcs_secret) .gcs_secret(settings.gcs_access, settings.gcs_secret)
.load_folder_gcs(settings.gcs_bucketname, sid) .load_folder_gcs(settings.gcs_bucketname, sid)
) )
@ -32,7 +33,11 @@ async def query(sid: str = "", q: str = "") -> str:
] ]
) )
print(prompt)
chat = await chat.eval("You're an expert sql developer", prompt) chat = await chat.eval("You're an expert sql developer", prompt)
query = extract_code_block(chat.last_response_content()) query = extract_code_block(chat.last_response_content())
result = str(db.query(query))
print(result)
return str(db.query(query)) return result

View file

@ -25,11 +25,9 @@ async def upload_file(file: UploadFile = File(...), sid: str = ""):
await f.write(content) await f.write(content)
await f.flush() await f.flush()
gcs.makedir(f"{settings.gcs_bucketname}/{sid}")
print("successfully created directory")
( (
DDB() DDB()
.gcs_secret(settings.gcs_secret, settings.gcs_secret) .gcs_secret(settings.gcs_access, settings.gcs_secret)
.load_metadata(f.name) .load_metadata(f.name)
.dump_gcs(settings.gcs_bucketname, sid) .dump_gcs(settings.gcs_bucketname, sid)
) )

View file

@ -28,27 +28,27 @@ textarea.addEventListener('input', function () {
}); });
// Function to fetch response // Function to fetch response
async function fetchResponse(message) { async function fetchResponse(message, newMessage) {
try { try {
const response = await fetch('/greetings'); const response = await fetch('/query?sid=' + sessionStorage.getItem('helloComputerSession') + '&q=' + message);
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText); throw new Error('Network response was not ok ' + response.statusText);
} }
const data = await response.text(); const data = await response.text();
// Hide spinner and display result // Hide spinner and display result
message.innerHTML = '<img src="/img/assistant.webp" width="50px"> <div>' + data + '</div>'; newMessage.innerHTML = '<img src="/img/assistant.webp" width="50px"> <div><pre>' + data + '</pre></div>';
} catch (error) { } catch (error) {
message.innerHTML = '<img src="/img/assistant.webp" width="50px">' + 'Error: ' + error.message; newMessage.innerHTML = '<img src="/img/assistant.webp" width="50px">' + 'Error: ' + error.message;
} }
} }
function addAIMessage() { function addAIMessage(messageContent) {
const newMessage = document.createElement('div'); const newMessage = document.createElement('div');
newMessage.classList.add('message', 'bg-white', 'p-2', 'mb-2', 'rounded'); newMessage.classList.add('message', 'bg-white', 'p-2', 'mb-2', 'rounded');
newMessage.innerHTML = '<img src="/img/assistant.webp" width="50px"> <div id="spinner" class="spinner"></div>'; newMessage.innerHTML = '<img src="/img/assistant.webp" width="50px"> <div id="spinner" class="spinner"></div>';
chatMessages.prepend(newMessage); // Add new message at the top chatMessages.prepend(newMessage); // Add new message at the top
fetchResponse(newMessage); fetchResponse(messageContent, newMessage);
} }
function addAIManualMessage(m) { function addAIManualMessage(m) {
@ -68,7 +68,7 @@ function addUserMessage() {
textarea.value = ''; // Clear the textarea textarea.value = ''; // Clear the textarea
textarea.style.height = 'auto'; // Reset the textarea height textarea.style.height = 'auto'; // Reset the textarea height
textarea.style.overflowY = 'hidden'; textarea.style.overflowY = 'hidden';
addAIMessage(); addAIMessage(messageContent);
} }
}; };

Binary file not shown.

View file

@ -23,7 +23,7 @@ def test_load():
assert db.sheets == ("answers",) assert db.sheets == ("answers",)
results = db.query("select * from answers").fetchall() results = db.query("select * from answers").fetchall()
assert len(results) == 2 assert len(results) == 6
def test_load_description(): def test_load_description():