guidance icon indicating copy to clipboard operation
guidance copied to clipboard

list of select based on other field

Open clausagerskov opened this issue 9 months ago • 3 comments

Is it possible to generate a field containing a list of selects based on another list of strings? Say the prompt is "My name is John, I like going to Spain and Germany but not Greece or Italy", I would like to create an extractor function that generates the json: { "name": "John", "countries": ["Spain", "Germany", "Greece", "Italy"], "country_sentiment": ["Positive", "Positive", "Negative", "Negative"] } I understand that select can be used with the options to restrict to "Positive" and "Negative", but not how the selection can be made to depend on another field, both in terms of number of items and actual selection of values.

clausagerskov avatar Apr 29 '24 14:04 clausagerskov

Hi @clausagerskov,

You can interleave arbitrary python code into your guidance functions, which might help with this use case. That means you can pass Python lists in as elements to the select, or use conditionals, loops inside your functions, etc. There's many ways to approach your specific task -- what I have here isn't the best way, but just to give you a sense of the guidance/python integration:

country_list = ["Spain", "Germany", "Greece", "Italy"]
country_sentiment = []
lm += "My name is John, I like going to Spain and Germany but not Greece or Italy."
for country in country_list:
    lm2 = lm + f" Based on this information, please write the sentiment of the country as 'Positive' or 'Negative'. Country: {country}, sentiment: {select(['Positive', 'Negative'], name='sentiment')}." 
    country_sentiment.append(lm2['sentiment'])
return_dict = {"name": "John", "countries": country_list, "country_sentiment": country_sentiment}

(Apologies for any syntax errors, writing this on the go!)

Harsha-Nori avatar May 02 '24 04:05 Harsha-Nori

Okay but how do I also extract the country_list in the same guidance call?

clausagerskov avatar May 02 '24 07:05 clausagerskov

If you mean that you want to generate the JSON directly, then you can define a schema for it. However, you'd probably then need to have some examples to get the model to understand what you want. Doing something close to what @Harsha-Nori showed is probably going to be cheapest token-wise.

riedgar-ms avatar May 14 '24 13:05 riedgar-ms