cohere-ai / cohere-python

Python Library for Accessing the Cohere API
https://docs.cohere.ai
MIT License
302 stars 65 forks source link

Force Cohere LLM to generate only Json format as expected without any other unnecessary text #394

Closed thnguyendn closed 7 months ago

thnguyendn commented 8 months ago

Hello,

I do not really know if it is a right place to ask my question, but did not find any appropriate ones.

I would like to know if when we ask Cohere to generate text completion (or chat) through API, do we have a solution to force Cohere to generate only Json validated format without any introduction text at the beginning ?

I have used a prompt-engineering approach to require a Json validated-format in my prompt by adding either : "your output is only of json valid structure [{{'key1': value1, 'keys2': [values], 'key3': value3}}]" Or "your output is only of json valid structure [{{'key1': value1, 'keys2': [values], 'key3': value3}}], without any introduction text ..."

As result, Cohere always add an introduction text at the beginning as follows : "Here is the JSON format generated from the information provided: '''json [useful_information_situated_here] ''' "

What I really want to receive as output is only the Json valid format inside of parenthesis (i.e. useful_information_situated_here)

aanaseer commented 7 months ago

I think this article on validating LLM outputs using Guardrails AI might be helpful.

elaineg commented 7 months ago

Hi, thanks for filing this ticket, it's on our roadmap to add forced json outputs.

thnguyendn commented 7 months ago

thank you for all of your replies @aanaseer @elaineg. Looking forward to test it later...