dify
dify copied to clipboard
ChatApi | How to get total_price from AppGenerateService.generate() --> response
Self Checks
- [X] I have searched for existing issues search for existing issues, including closed ones.
- [X] I confirm that I am using English to submit report (我已阅读并同意 Language Policy).
- [X] Please do not modify this template :) and fill in all the required fields.
Provide a description of requested docs changes
Need help to get total_price once AppGenerateService.generate() --> response
Path of file is: https://github.com/langgenius/dify/blob/main/api/controllers/web/completion.py
class ChatApi(WebApiResource):
def post(self, app_model, end_user):
app_mode = AppMode.value_of(app_model.mode)
if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
raise NotChatAppError()
parser = reqparse.RequestParser()
parser.add_argument('inputs', type=dict, required=True, location='json')
parser.add_argument('query', type=str, required=True, location='json')
parser.add_argument('files', type=list, required=False, location='json')
parser.add_argument('response_mode', type=str, choices=['blocking', 'streaming'], location='json')
parser.add_argument('conversation_id', type=uuid_value, location='json')
parser.add_argument('retriever_from', type=str, required=False, default='web_app', location='json')
args = parser.parse_args()
streaming = args['response_mode'] == 'streaming'
args['auto_generate_name'] = False
try:
response = AppGenerateService.generate(
app_model=app_model,
user=end_user,
args=args,
invoke_from=InvokeFrom.WEB_APP,
streaming=streaming
)
# How can i get total_price from AppGenerateService.generate() --> response
#total_price = response?
return helper.compact_generate_response(response)
except services.errors.conversation.ConversationNotExistsError:
raise NotFound("Conversation Not Exists.")
except services.errors.conversation.ConversationCompletedError:
raise ConversationCompletedError()
except services.errors.app_model_config.AppModelConfigBrokenError:
logging.exception("App model config broken.")
raise AppUnavailableError()
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description)
except QuotaExceededError:
raise ProviderQuotaExceededError()
except ModelCurrentlyNotSupportError:
raise ProviderModelCurrentlyNotSupportError()
except InvokeError as e:
raise CompletionRequestError(e.description)
except ValueError as e:
raise e
except Exception as e:
logging.exception(f"{e} - internal server error.")
raise InternalServerError()
`
To get the total_price
from the response
object returned by AppGenerateService.generate()
, you can access it directly if it's a key in the response dictionary. Here's how you could do it:
total_price = response.get('total_price') # Safely get total_price, returns None if not found
This approach uses the .get()
method to avoid a KeyError
if the total_price
key does not exist in the response
. Ensure that the response
structure actually includes a total_price
field. If total_price
is nested or the response structure is different (e.g., a custom class), you'll need to adjust the access method accordingly.
Details
Sources
- api/controllers/web/completion.py
- api/services/app_generate_service.py
- sdks/nodejs-client/index.d.ts
- api/core/app/apps/base_app_generate_response_converter.py
- api/core/app/apps/workflow/app_generator.py
- api/core/app/task_pipeline/based_generate_task_pipeline.py
- api/core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py
- api/core/app/apps/workflow/generate_task_pipeline.py
- api/core/application_manager.py
About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.