David commited on
Commit
1757136
·
1 Parent(s): f12abfa

Add model instructions and output format

Browse files
Files changed (1) hide show
  1. main.py +26 -6
main.py CHANGED
@@ -17,6 +17,8 @@ app = FastAPI()
17
 
18
  class Prompt(BaseModel):
19
  text: str
 
 
20
 
21
  def query(payload):
22
  response = requests.post(API_URL, headers=headers, json=payload)
@@ -28,13 +30,31 @@ def read_root():
28
 
29
  @app.post("/generate")
30
  async def generate_text(prompt: Prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  response = query({
32
- "messages": [
33
- {
34
- "role": "user",
35
- "content": prompt.text
36
- }
37
- ],
38
  "model": "meta-llama/Llama-3.2-3B-Instruct"
39
  })
 
40
  return {"response": response["choices"][0]["message"]}
 
17
 
18
  class Prompt(BaseModel):
19
  text: str
20
+ instructions: str | None = None # optional system instructions
21
+ output_format: str | None = None
22
 
23
  def query(payload):
24
  response = requests.post(API_URL, headers=headers, json=payload)
 
30
 
31
  @app.post("/generate")
32
  async def generate_text(prompt: Prompt):
33
+ messages = []
34
+
35
+ # Add system instructions if provided
36
+ if prompt.instructions:
37
+ messages.append({
38
+ "role": "system",
39
+ "content": prompt.instructions
40
+ })
41
+
42
+ # Add user message
43
+ user_content = prompt.text
44
+
45
+ # If an output format is specified, append formatting instructions
46
+ if prompt.output_format:
47
+ user_content += f"\n\nPlease always format the response as: {prompt.output_format}"
48
+
49
+ messages.append({
50
+ "role": "user",
51
+ "content": user_content
52
+ })
53
+
54
+ # Call HF API
55
  response = query({
56
+ "messages": messages,
 
 
 
 
 
57
  "model": "meta-llama/Llama-3.2-3B-Instruct"
58
  })
59
+
60
  return {"response": response["choices"][0]["message"]}