Spaces:
Paused
Paused
api updated
Browse files- api/api.py +28 -11
- api/prompts/system_prompt.txt +4 -1
- app.py +0 -2
api/api.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
|
| 4 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
# from api.CREDS import GROQ_API_KEY
|
| 7 |
|
| 8 |
-
class
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
@@ -17,13 +22,25 @@ with open("./api/prompts/system_prompt.txt", "r") as f:
|
|
| 17 |
system = f.read()
|
| 18 |
|
| 19 |
human = "{text}"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.post("/chat/")
|
| 27 |
-
async def chat(input_data:
|
| 28 |
-
res = chain.invoke(input_data['text'])
|
| 29 |
-
return {"response": res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
|
| 4 |
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from langchain_core.prompts import PromptTemplate
|
| 6 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
| 7 |
+
from langchain_core.output_parsers import JsonOutputParser
|
| 8 |
+
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
# from api.CREDS import GROQ_API_KEY
|
| 11 |
|
| 12 |
+
class ChatOutput(BaseModel):
|
| 13 |
+
score: int = Field(description="score of chats out of 100")
|
| 14 |
+
description: str = Field(description="short description on how the score was given")
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
| 22 |
system = f.read()
|
| 23 |
|
| 24 |
human = "{text}"
|
| 25 |
+
|
| 26 |
+
parser = JsonOutputParser(pydantic_object=ChatOutput)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
prompt = PromptTemplate(
|
| 30 |
+
template=system,
|
| 31 |
+
input_variables=['user_query'],
|
| 32 |
+
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 33 |
+
)
|
| 34 |
+
chain = prompt | llm | parser
|
| 35 |
|
| 36 |
@app.post("/chat/")
|
| 37 |
+
async def chat(input_data: ChatOutput):
|
| 38 |
+
res = chain.invoke({"user_query": input_data['text']})
|
| 39 |
+
return {"response": res}
|
| 40 |
+
|
| 41 |
+
# with open('./api/chats/good_chat.txt', 'r') as f:
|
| 42 |
+
# chat = f.read()
|
| 43 |
+
|
| 44 |
+
# result = chain.invoke({"user_query": chat})
|
| 45 |
+
|
| 46 |
+
# print(result)
|
api/prompts/system_prompt.txt
CHANGED
|
@@ -27,4 +27,7 @@ Contextual Cues:
|
|
| 27 |
Presence of urgent keywords (e.g., "urgent," "ASAP," "emergency").
|
| 28 |
Use of punctuation and capitalization to express intensity (e.g., exclamation marks, all caps).
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
Presence of urgent keywords (e.g., "urgent," "ASAP," "emergency").
|
| 28 |
Use of punctuation and capitalization to express intensity (e.g., exclamation marks, all caps).
|
| 29 |
|
| 30 |
+
---
|
| 31 |
+
{format_instructions}
|
| 32 |
+
---
|
| 33 |
+
{user_query}
|
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -18,7 +17,6 @@ app.add_middleware(
|
|
| 18 |
@app.post("/chat/")
|
| 19 |
async def chat_endpoint(input_data: dict):
|
| 20 |
response = await chat(input_data)
|
| 21 |
-
print("==> ", response)
|
| 22 |
return {"response": response['response']}
|
| 23 |
|
| 24 |
async def gradio_chat(text):
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 17 |
@app.post("/chat/")
|
| 18 |
async def chat_endpoint(input_data: dict):
|
| 19 |
response = await chat(input_data)
|
|
|
|
| 20 |
return {"response": response['response']}
|
| 21 |
|
| 22 |
async def gradio_chat(text):
|