Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taranjeet/llmformatter
Get deterministic output in any format like json from any LLM.
https://github.com/taranjeet/llmformatter
llm openai
Last synced: 3 months ago
JSON representation
Get deterministic output in any format like json from any LLM.
- Host: GitHub
- URL: https://github.com/taranjeet/llmformatter
- Owner: taranjeet
- License: mit
- Created: 2023-04-25T16:11:08.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-04-25T17:50:57.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T07:12:39.272Z (4 months ago)
- Topics: llm, openai
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 18
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# llmformatter
Get deterministic output in any format like json from any LLM.
## Installation
```bash
git clone [email protected]:taranjeet/llmformatter.git
cd llmformatter
python setup.py install
```## Examples
### Example 1: Get output as json
```python
import openai
from llmformatter import llm_formatteropenai.api_key = "sk-..."
# get output as json which can be parsed
prompt = """You need to provide a single random question along with the correct answer related to Naruto. You will generate a question, four options, one correct, three wrong. The options should have no labels like A, B, C or D. Options should be unique and should not contain repetitive or same value. Correct answer must exist in the options."""response_normal = openai.ChatCompletion.create(messages=[{"role": "user", "content": prompt}], model="gpt-3.5-turbo", temperature=0, top_p=1)
print(response_normal.choices[0].message.content)"""
Question: What is the name of the technique that allows Naruto to create multiple shadow clones of himself?Options:
- Rasengan
- Chidori
- Kage Bunshin no Jutsu
- AmaterasuCorrect answer: Kage Bunshin no Jutsu
"""# notice how the above output is not json and parsing this will be difficult
# now let's use the llm_formatter to get the json output onlyresponse_format = openai.ChatCompletion.create(messages=[{"role": "user", "content": llm_formatter(prompt, "json")}], model="gpt-3.5-turbo", temperature=0, top_p=1)
print(response_format.choices[0].message.content)
{
"question": "What is the name of the village where Naruto was born?",
"options": [
"Konohagakure",
"Sunagakure",
"Kirigakure",
"Iwagakure"
],
"answer": "Konohagakure"
}```
### Example 2: Get output as code
```python
import openai
from llmformatter import llm_formatteropenai.api_key = "sk-..."
# get output as code which can be written straightaway to a file and executed
prompt = "Write a python function to sum two numbers"response_normal = openai.ChatCompletion.create(messages=[{"role": "user", "content": prompt}], model="gpt-3.5-turbo", temperature=0, top_p=1)
print(response_normal.choices[0].message.content)"""
Here's a simple Python function that takes two numbers as input and returns their sum:```python
def add_numbers(num1, num2):
return num1 + num2
\```You can call this function with any two numbers you want, like this:
```python
result = add_numbers(5, 7)
print(result) # Output: 12
\```In this example, we're passing the numbers 5 and 7 to the `add_numbers` function, which returns their sum (12). We're then printing the result to the console using the `print` function.
"""
# notice how the above output contains both code and text
# now lets use llm_formatter to only get the code output for above exampleresponse_format = openai.ChatCompletion.create(messages=[{"role": "user", "content": llm_formatter(prompt, "code")}], model="gpt-3.5-turbo", temperature=0, top_p=1)
print(response_format.choices[0].message.content)def sum_numbers(a, b):
return a + b
```## Available formats
### json
* Returns the format as json.
* Can be used as```python
from llmformatter import llm_formatterllm_formatter(prompt, "json")
```### code
* Returns the format as code.
* Can be used as```python
from llmformatter import llm_formatterllm_formatter(prompt, "code")
```