Skip to content

Commit

Permalink
Refactor (examples/): remove response structure of llm example apps
Browse files Browse the repository at this point in the history
  • Loading branch information
aybruhm committed May 20, 2024
1 parent abaa09c commit 264dbbc
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 50 deletions.
7 changes: 1 addition & 6 deletions examples/async_chat_sdk_output_format/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,4 @@ async def chat(inputs: MessagesInput = MessagesInput()):
temperature=ag.config.temperature,
max_tokens=max_tokens,
)
token_usage = chat_completion.usage.dict()
return {
"message": chat_completion.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return chat_completion.choices[0].message.content
7 changes: 1 addition & 6 deletions examples/async_startup_technical_ideas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,4 @@ async def chat(inputs: MessagesInput = MessagesInput()) -> str:
temperature=ag.config.temperature,
max_tokens=max_tokens,
)
token_usage = chat_completion.usage.dict()
return {
"message": chat_completion.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return chat_completion.choices[0].message.content
9 changes: 2 additions & 7 deletions examples/baby_name_generator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Give me 10 names for a baby from this country {country} with gender {gender}!!!!"
)

ag.init(app_name="test", base_name="app")
ag.init()
ag.config.default(
temperature=FloatParam(0.2), prompt_template=TextParam(default_prompt)
)
Expand All @@ -31,9 +31,4 @@ def generate(country: str, gender: str) -> str:
chat_completion = client.chat.completions.create(
model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]
)
token_usage = chat_completion.usage.dict()
return {
"message": chat_completion.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage("gpt-3.5-turbo", token_usage),
}
return chat_completion.choices[0].message.content
7 changes: 1 addition & 6 deletions examples/chat_json_format/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ def chat(inputs: ag.MessagesInput = ag.MessagesInput()):
max_tokens=max_tokens,
response_format=response_format,
)
token_usage = chat_completion.usage.dict()
return {
"message": chat_completion.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return chat_completion.choices[0].message.content
7 changes: 1 addition & 6 deletions examples/extract_data_to_json/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,5 @@ def generate(
temperature=ag.config.temperature,
functions=[function],
)
token_usage = response.usage.dict()
output = str(response["choices"][0]["message"]["function_call"])
return {
"message": output,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return output
7 changes: 1 addition & 6 deletions examples/startup_technical_ideas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ def chat(inputs: MessagesInput = MessagesInput()) -> str:
temperature=ag.config.temperature,
max_tokens=max_tokens,
)
token_usage = chat_completion.usage.dict()
return {
"message": chat_completion.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return chat_completion.choices[0].message.content
7 changes: 1 addition & 6 deletions examples/vision_gpt_explain_image/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,4 @@ def explain(
messages=messages,
max_tokens=ag.config.max_tokens,
)
token_usage = chat_completion.usage.dict()
return {
"message": chat_completion.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return chat_completion.choices[0].message.content
8 changes: 1 addition & 7 deletions examples/what_to_do_today/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,4 @@ async def generate(
completion_params["presence_penalty"] = ag.config.presence_penalty

response = await litellm.acompletion(**completion_params)

token_usage = response.usage.dict()
return {
"message": response.choices[0].message.content,
**{"usage": token_usage},
"cost": ag.calculate_token_usage(ag.config.model, token_usage),
}
return response.choices[0].message.content

0 comments on commit 264dbbc

Please sign in to comment.