Find answers from the community

Updated 10 months ago

how to invoke an API?

@kapa.ai how to invoke an API?
k
2 comments
Please stand by @vigneshzoro as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To invoke an API using LlamaIndex, you first need to obtain an API key from the respective service. For example, if you're using the Llama API, you can obtain the key from

Once you have the API key, you can use it to initialize the API object in your Python code. Here's an example of how to do this with the Llama API:

Plain Text
from llama_index.llms.llama_api import LlamaAPI

api_key = "LL-your-key"  # replace with your actual API key
llm = LlamaAPI(api_key=api_key)


After initializing the API object, you can use its methods to interact with the API. For example, you can use the complete method to get a completion for a given prompt:

Plain Text
resp = llm.complete("Paul Graham is ")
print(resp)


Or you can use the chat method to have a conversation with the API:

Plain Text
from llama_index.llms import ChatMessage

messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality"
    ),
    ChatMessage(role="user", content="What is your name"),
]
resp = llm.chat(messages)
print(resp)


Remember to replace "LL-your-key" with your actual API key.

For more details, you can refer to the Llama API documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord