guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
19.1k stars 1.04k forks source link

got error : module, class, method, function, traceback, frame, or code object was expected, got str when using AzureOpenAI #538

Open jacksonhu opened 11 months ago

jacksonhu commented 11 months ago

my code is below:

guidance.llm=guidance.models.AzureOpenAI(
    model='gpt-35-turbo',
    api_key='xxx',
    azure_endpoint='https://xxxx.openai.azure.com/',
    api_version='2023-07-01-preview',
    deployment_id='gpt-35-turbo'
)
valid_weapons = ["sword", "axe", "mace", "spear", "bow", "crossbow"]
valid_armors = ['leather', 'chainmail', 'plate']

character_maker = guidance("""The following is a character profile for an RPG game in JSON format.
```json
{
    "id": "{{id}}",
    "description": "{{description}}",
    "name": "{{gen 'name'}}",
    "age": {{gen 'age' stop=','}},
    "armor": "{{select 'armor' options=valid_armors}}",
    "weapon": "{{select 'weapon' options=valid_weapons}}",
    "class": "{{gen 'class'}}",
    "mantra": "{{gen 'mantra' temperature=0.7}}",
    "strength": {{gen 'strength' stop=','}},
    "items": [{{#geneach 'items' num_iterations=5 join=', '}}"{{gen 'this' temperature=0.7}}"{{/geneach}}]
}```""")

output = character_maker(
    id="e1f491f7-7ab8-4dac-8c20-c92b5e7d883d",
    description="A quick and nimble fighter.",
    valid_weapons=valid_weapons,
    valid_armors=valid_armors
)
print(output)

and I got error : Traceback (most recent call last): File "/Users/loganhu/Documents/eclipse_workspace/ai4dataops/py-server/ai4dataops/guidance/Test2.py", line 21, in character_maker = guidance("""The following is a character profile for an RPG game in JSON format. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/loganhu/Documents/eclipse_workspace/ai4dataops/py-server/venv/lib/python3.11/site-packages/guidance-0.1.7-py3.11-macosx-12-x86_64.egg/guidance/init.py", line 25, in call return _decorator(f, stateless=stateless, cache=cache, dedent=dedent, model=model) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/loganhu/Documents/eclipse_workspace/ai4dataops/py-server/venv/lib/python3.11/site-packages/guidance-0.1.7-py3.11-macosx-12-x86_64.egg/guidance/init.py", line 43, in _decorator f = strip_multiline_string_indents(f) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/loganhu/Documents/eclipse_workspace/ai4dataops/py-server/venv/lib/python3.11/site-packages/guidance-0.1.7-py3.11-macosx-12-x86_64.egg/guidance/_utils.py", line 75, in strip_multiline_string_indents source = textwrap.dedent(inspect.getsource(f)) ^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 1262, in getsource lines, lnum = getsourcelines(object) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 1244, in getsourcelines lines, lnum = findsource(object) ^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 1063, in findsource file = getsourcefile(object) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 940, in getsourcefile filename = getfile(object) ^^^^^^^^^^^^^^^ File "/Users/loganhu/Documents/eclipse_workspace/ai4dataops/py-server/venv/lib/python3.11/site-packages/torch/package/package_importer.py", line 696, in _patched_getfile return _orig_getfile(object) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 920, in getfile raise TypeError('module, class, method, function, traceback, frame, or ' TypeError: module, class, method, function, traceback, frame, or code object was expected, got str

guidance version 0.1.7 openai sdk version is 1.3.8 how could I fix this problem?

Harsha-Nori commented 11 months ago

Hey @jacksonhu, this is the old guidance handlebars syntax that isn't supported in the new version of the library (v0.1+). We're currently maintaining the more pythonic version of the codebase. Your best bet is to run examples from our README -- in your case, here: https://github.com/guidance-ai/guidance#guidance-acceleration

Make sure you initialize new models in the new format too: https://github.com/guidance-ai/guidance#openai

mcchung52g commented 11 months ago

I feel new syntax is making it little harder to incorporate guidance into things like autogen. I'm having a hard time mixing, or even visualizing, the two together... and both are Microsoft products :(

at least up to this point in time, I felt guidance could've been the hope and gap-filler for open source LLMs. I mean openai handles things well and majority of examples are and builds on it. But once you move away from it, not only there are less resource but open LLMs have a functional gap that makes using any agent framework less useful. or at least I'm having trouble making it useful...

mcchung52g commented 11 months ago

@Harsha-Nori how would i create 3 different characters? below is only producing the same thing 3 times with only the id being different: @guidance def character_maker(lm, id, description, valid_weapons): lm += f"""\ The following is a character profile for an RPG game in JSON format.


    {{
        "id": "{id}",
        "description": "{description}",
        "name": "{gen('name', stop='"')}",
        "age": {gen('age', regex='[0-9]+', stop=',')},
        "armor": "{select(options=['leather', 'chainmail', 'plate'], name='armor')}",
        "weapon": "{select(options=valid_weapons, name='weapon')}",
        "class": "{gen('class', stop='"')}",
        "mantra": "{gen('mantra', stop='"')}",
        "strength": {gen('strength', regex='[0-9]+', stop=',')},
        "items": ["{gen('item', list_append=True, stop='"')}", "{gen('item', list_append=True, stop='"')}", "{gen('item', list_append=True, stop='"')}"]
    }}```"""
    return lm
for x in range(3):
    llama2 += character_maker(x, 'A nimble fighter', ['axe', 'sword', 'bow'])
print(llama2)
younes-io commented 10 months ago

FYI https://github.com/guidance-ai/guidance/issues/584