Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
+
# Assuming Llama class has been correctly imported and set up
|
| 5 |
+
from llama_cpp import Llama
|
| 6 |
+
|
| 7 |
+
# Model loading with specified path and configuration
|
| 8 |
+
llm = Llama(
|
| 9 |
+
model_path="Anoop03031988/Phi-3-mini-4k-instruct-text-to-sql-GGUF", # Update the path as necessary
|
| 10 |
+
n_ctx=4096, # Maximum number of tokens for context (input + output)
|
| 11 |
+
n_threads=2, # Number of CPU cores used
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Pydantic object for validation
|
| 15 |
+
class Validation(BaseModel):
|
| 16 |
+
user_prompt: str
|
| 17 |
+
system_prompt: str
|
| 18 |
+
max_tokens: int = 1024
|
| 19 |
+
temperature: float = 0.01
|
| 20 |
+
|
| 21 |
+
# FastAPI application initialization
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
# Endpoint for generating responses
|
| 25 |
+
@app.post("/generate_response")
|
| 26 |
+
async def generate_response(item: Validation):
|
| 27 |
+
# Construct the complete prompt using the given system and user prompts
|
| 28 |
+
prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|> \n
|
| 29 |
+
{ item.system_prompt }<|eot_id|> \n <|start_header_id|>user<|end_header_id|>
|
| 30 |
+
{ item.user_prompt }<|eot_id|> \n <|start_header_id|>assistant<|end_header_id|>"""
|
| 31 |
+
|
| 32 |
+
# Call the Llama model to generate a response
|
| 33 |
+
output = llm(prompt, max_tokens = item.max_tokens,temperature = item.temperature, echo=True)
|
| 34 |
+
|
| 35 |
+
# Extract and return the text from the response
|
| 36 |
+
return output['choices'][0]['text']
|