Let's start from very beginning. We assume you already installed Python (with venv), if not, here a guide for the beginners.
Create a new folder for test project, name it as aimlapi-welcome
and change to it.
mkdir ./aimlapi-welcome
cd ./aimlapi-welcome
(Optional) If you use IDE then we recommend to open created folder as workspace. On example, in VSCode you can do it with:
code .
Run a terminal inside created folder and create virtual envorinment with a command
python3 -m venv ./.venv
Activate created virtual environment
# Linux / Mac
source ./.venv/bin/activate
# Windows
./.venv/bin/Activate.bat
Install requirement dependencies. In our case we need only OpenAI SDK
pip install openai
Create new file and name it as travel.py
touch travel.py
Paste following content inside this travel.py
and replace my_key
with your API key you got on first step
from openai import OpenAI
base_url = "<https://api.aimlapi.com/v1>"
api_key = "my_key"
system_prompt = "You are a travel agent. Be descriptive and helpful."
user_prompt = "Tell me about San Francisco"
api = OpenAI(api_key=api_key, base_url=base_url)
def main():
completion = api.chat.completions.create(
model="mistralai/Mistral-7B-Instruct-v0.2",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.7,
max_tokens=256,
)
response = completion.choices[0].message.content
print("User:", user_prompt)
print("AI:", response)
if __name__ == "__main__":
main()
Run the application
python3 ./travel.py
If you done all correct, you will see following output:
User: Tell me about San Francisco
AI: San Francisco, located in northern California, USA, is a vibrant and culturally rich city known for its iconic landmarks, beautiful vistas, and diverse neighborhoods. It's a popular tourist destination famous for its iconic Golden Gate Bridge, which spans the entrance to the San Francisco Bay, and the iconic Alcatraz Island, home to the infamous federal prison.
The city's famous hills offer stunning views of the bay and the cityscape. Lombard Street, the "crookedest street in the world," is a must-see attraction, with its zigzagging pavement and colorful gardens. Ferry Building Marketplace is a great place to explore local food and artisanal products, and the Pier 39 area is home to sea lions, shops, and restaurants.
San Francisco's diverse neighborhoods each have their unique character. The historic
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article