Created
June 20, 2025 08:25
-
-
Save stevedoyle/3cba74fc871a81e6707a0ab5945e023f to your computer and use it in GitHub Desktop.
A simple example of how to send prompts to an LLM using LiteLLM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple example of how to send prompts to an LLM using LiteLLM: | |
# | |
# Set your API key as shown in the Google Colab | |
# | |
import os | |
from google.colab import userdata | |
api_key = userdata.get('OPENAI_API_KEY') | |
os.environ['OPENAI_API_KEY'] = api_key | |
from litellm import completion | |
from typing import List, Dict | |
def generate_response(messages: List[Dict]) -> str: | |
"""Call LLM to get response""" | |
response = completion( | |
model="openai/gpt-4o", | |
messages=messages, | |
max_tokens=1024 | |
) | |
return response.choices[0].message.content | |
messages = [ | |
{"role": "system", "content": "You are an expert software engineer that prefers functional programming."}, | |
{"role": "user", "content": "Write a function to swap the keys and values in a dictionary."} | |
] | |
response = generate_response(messages) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment