diff --git a/requirements.in b/requirements.in index 8048479..e6d6efa 100644 --- a/requirements.in +++ b/requirements.in @@ -1,2 +1,5 @@ langchain +langchain-community +openai fastapi +pydantic-settings diff --git a/src/hellocomputer/config.py b/src/hellocomputer/config.py new file mode 100644 index 0000000..6e90838 --- /dev/null +++ b/src/hellocomputer/config.py @@ -0,0 +1,12 @@ +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + anyscale_api_key: str = "Awesome API" + gcs_access: str = "access" + gcs_secret: str = "secret" + + model_config = SettingsConfigDict(env_file=".env") + + +settings = Settings() diff --git a/src/hellocomputer/main.py b/src/hellocomputer/main.py index 9ff9e09..3396e20 100644 --- a/src/hellocomputer/main.py +++ b/src/hellocomputer/main.py @@ -1,8 +1,12 @@ from fastapi import FastAPI, status from fastapi.staticfiles import StaticFiles +from fastapi.responses import PlainTextResponse import hellocomputer from pathlib import Path from pydantic import BaseModel +from langchain_community.chat_models import ChatAnyscale +from langchain_core.messages import HumanMessage, SystemMessage +from .config import settings static_path = Path(hellocomputer.__file__).parent / "static" @@ -36,6 +40,30 @@ def get_health() -> HealthCheck: return HealthCheck(status="OK") +@app.get("/greetings", response_class=PlainTextResponse) +async def get_greeting() -> str: + model = "meta-llama/Meta-Llama-3-8B-Instruct" + chat = ChatAnyscale( + model_name=model, + temperature=0.5, + anyscale_api_key=settings.anyscale_api_key, + ) + + messages = [ + SystemMessage(content="You are a helpful AI that shares everything you know."), + HumanMessage( + content="Make a short presentation of yourself " + "as an assistant in Spanish in about 20 words. " + "You're capable of analyzing a file that a user " + "has previously uploaded." + ), + ] + + model_response = await chat.ainvoke(messages) + print(model_response.response_metadata) + return model_response.content + + app.mount( "/", StaticFiles(directory=static_path, html=True, packages=["bootstrap4"]), diff --git a/src/hellocomputer/static/.DS_Store b/src/hellocomputer/static/.DS_Store new file mode 100644 index 0000000..8f0779e Binary files /dev/null and b/src/hellocomputer/static/.DS_Store differ diff --git a/src/hellocomputer/static/img/assistant.webp b/src/hellocomputer/static/img/assistant.webp new file mode 100644 index 0000000..9ad3e0a Binary files /dev/null and b/src/hellocomputer/static/img/assistant.webp differ diff --git a/src/hellocomputer/static/index.html b/src/hellocomputer/static/index.html index b1b40d2..e07d108 100644 --- a/src/hellocomputer/static/index.html +++ b/src/hellocomputer/static/index.html @@ -7,9 +7,6 @@ Chat Application - @@ -35,14 +32,43 @@ +
+ + + + +
-
Message 1
-
Message 2
-
Message 3
+
+ +
+
+ +
+
@@ -63,6 +89,9 @@ + diff --git a/src/hellocomputer/static/script.js b/src/hellocomputer/static/script.js index 42c0a74..a9c79d4 100644 --- a/src/hellocomputer/static/script.js +++ b/src/hellocomputer/static/script.js @@ -47,4 +47,33 @@ textarea.addEventListener('keypress', function (e) { e.preventDefault(); addUserMessage(); } -}); \ No newline at end of file +}); + +document.addEventListener("DOMContentLoaded", function () { + // Elements + const spinner = document.getElementById('spinner'); + const resultDiv = document.getElementById('result'); + + // Function to fetch greeting + async function fetchGreeting() { + try { + const response = await fetch('/greetings'); + if (!response.ok) { + throw new Error('Network response was not ok ' + response.statusText); + } + const data = await response.text(); + + // Hide spinner and display result + spinner.classList.add('hidden'); + resultDiv.classList.remove('hidden'); + resultDiv.textContent = data; + } catch (error) { + spinner.classList.add('hidden'); + resultDiv.classList.remove('hidden'); + resultDiv.textContent = 'Error: ' + error.message; + } + } + + // Call the function to fetch greeting + fetchGreeting(); +}); diff --git a/src/hellocomputer/static/style.css b/src/hellocomputer/static/style.css index aa33491..8345961 100644 --- a/src/hellocomputer/static/style.css +++ b/src/hellocomputer/static/style.css @@ -64,4 +64,29 @@ html { border-top: 1px solid #ddd; padding: 10px; box-sizing: border-box; +} + + +.spinner { + border: 4px solid #f3f3f3; + border-top: 4px solid #3498db; + border-radius: 50%; + width: 20px; + height: 20px; + animation: spin 2s linear infinite; + margin: auto; +} + +@keyframes spin { + 0% { + transform: rotate(0deg); + } + + 100% { + transform: rotate(360deg); + } +} + +.hidden { + display: none; } \ No newline at end of file