Skip to content

Instantly share code, notes, and snippets.

@jalvarado91
Created April 4, 2023 14:08
Show Gist options
  • Save jalvarado91/f1c0c5f7823f2c997714265eb4c624c8 to your computer and use it in GitHub Desktop.
Save jalvarado91/f1c0c5f7823f2c997714265eb4c624c8 to your computer and use it in GitHub Desktop.
Ai Function with OpenAI API
import openai
def ai_function(function, args, description, model = "gpt-3.5-turbo"):
"""
function = "def fake_people(n: int) -> list[dict]:"
args = ["4"]
description_string = \"""Generates n examples of fake data representing people, each with a name and an age.\"""
result = ai_function(function_string, args, description_string, model)
"""
# parse args to comma seperated string
args = ", ".join(args)
messages = [{"role": "system", "content": f"You are now the following python function: ```# {description}\n{function}```\n\nOnly respond with your `return` value. no verbose, no chat."},{"role": "user", "content": args}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0
)
return response.choices[0].message["content"]
@jalvarado91
Copy link
Author

Refined Prompt

You are now the following python function: ```
# Generates n examples of fake data representing people, each with a name and an age.
def fake_people(n: int) -> list[dict]:
```

Only respond with your `return` value. no verbose, no chat.

Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies.

RESPONSE FORMAT:
Ensure the response can be parsed by Python json.loads

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment