Brainverse_Beta is the backend built on FastAPI. To run backend follow these steps:
This documentation describes the steps each team member should follow to configure and test the provided FastAPI code, which uses the C4AI Command R+ 08-2024 model. Follow these steps carefully to ensure the server and APIs function correctly.
Before you start, make sure you have:
API_TOKEN).fastapiuvicorntransformerstorch (or PyTorch)If you don't have them, you can install them with the command:
pip install fastapi uvicorn transformers torch
Create a virtual environment:
python -m venv .venv
.\.venv\Scripts\activate
python3 -m venv .venv
source .venv/bin/activate
Add the code to the main.py file:
main.py.main.py file.Configure your Hugging Face API token:
API_TOKEN with your valid token.Start the FastAPI server with Uvicorn:
uvicorn main:app --reload
main is the name of the file without the .py extension.--reload option allows the server to update automatically after code changes.Verify that the server is running:
http://127.0.0.1:8000.{
"message": "The server is up and running!"
}
Main Endpoint for Content Generation:
http://127.0.0.1:8000/generate-content{
"prompt": "Write an article about the impact of AI on education."
}
{
"articles": [
{
"title": "Generated Article",
"description": "This article was generated based on the provided prompt.",
"content": "Artificial intelligence is revolutionizing education by..."
}
]
}
Debug Endpoint (Optional):
http://127.0.0.1:8000/debug{
"prompt": "Test debug prompt"
}
{
"prompt_received": "Test debug prompt"
}
If you encounter an error related to PyTorch, install it with:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Ensure your Hugging Face token is correct and active. You can verify this by accessing your Hugging Face account.
/generate-content endpoint.If you need further clarification or support, don't hesitate to ask! π
Here is the complete code to be added to the main.py file:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
from transformers import AutoTokenizer, AutoModelForCausalLM
# Hugging Face model configuration
API_TOKEN = "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" # Replace with your valid token
MODEL_ID = "CohereForAI/c4ai-command-r-plus-08-2024"
try:
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_auth_token=API_TOKEN)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, use_auth_token=API_TOKEN)
print("Model loaded successfully!")
except Exception as e:
print(f"Error loading the model: {e}")
raise HTTPException(status_code=500, detail="Error loading the C4AI Command R+ 08-2024 model.")
# Define input/output models for FastAPI
class Article(BaseModel):
title: str
description: str
content: str
class ArticleResponse(BaseModel):
articles: List[Article]
class PromptInput(BaseModel):
prompt: str
# Initialize the FastAPI application
app = FastAPI()
# Function to generate content using the model
def generate_content(prompt: str) -> str:
try:
print(f"Received prompt: {prompt}") # Debug log
# Define conversation input:
conversation = [
{"role": "user", "content": prompt}
]
# Define tools available for the model to use:
tools = [
{
"name": "internet_search",
"description": "Returns a list of relevant document snippets for a textual query retrieved from the internet",
"parameter_definitions": {
"query": {
"description": "Query to search the internet with",
"type": 'str',
"required": True
}
}
},
{
'name': "directly_answer",
"description": "Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history",
'parameter_definitions': {}
}
]
# Render the tool use prompt as a string:
tool_use_prompt = tokenizer.apply_tool_use_template(
conversation,
tools=tools,
tokenize=False,
add_generation_prompt=True,
)
print(tool_use_prompt)
# Prepare the message for the model
messages = [{"role": "user", "content": tool_use_prompt}]
input_ids = tokenizer.apply_chat_template(
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
)
# Generate text
gen_tokens = model.generate(
input_ids,
max_new_tokens=1000, # Maximum number of generated tokens
temperature=0.7, # Controls the randomness of generation
top_p=0.9, # Nucleus filtering
do_sample=True, # Allows generation based on sampling
)
# Decode the generated result
gen_text = tokenizer.decode(gen_tokens[0], skip_special_tokens=True)
print(f"Generated response: {gen_text[:200]}...") # Log the first 200 characters
return gen_text
except Exception as e:
print(f"Error generating content: {e}")
raise HTTPException(status_code=500, detail=f"LLM model error: {e}")
# Endpoint to generate content
@app.post("/generate-content", response_model=ArticleResponse)
def generate_content_endpoint(prompt_input: PromptInput):
prompt = prompt_input.prompt
print(f"Received prompt via API: {prompt}")
try:
# Generate content
generated_content = generate_content(prompt)
except HTTPException as e:
raise e
except Exception as e:
print(f"Unknown error: {e}")
raise HTTPException(status_code=500, detail="Unknown error during content generation.")
# Create the article with the generated content
article = Article(
title="Generated Article",
description="This article was generated based on the provided prompt.",
content=generated_content
)
return ArticleResponse(articles=[article])
# Test endpoint
@app.get("/")
def read_root():
return {"message": "The server is up and running!"}
# Debug endpoint (optional)
@app.post("/debug")
def debug_endpoint(data: PromptInput):
print(f"Received request: {data}")
return {"prompt_received": data.prompt}
The model we are using requires a prompt structured in a specific format to generate high-quality content. Following these guidelines will help you achieve the best possible results. Here is how the prompt should be structured:
{
"prompt": "Write an article about the applications of artificial intelligence in healthcare."
}
Prompt Key: This key contains the text of the prompt that describes the content you want to generate. The text should be clear and specific to achieve the best results.
{
"prompt": "Write an article about the impact of artificial intelligence on healthcare."
}
{
"prompt": "Provide a detailed recipe for making a classic Italian lasagna."
}
{
"prompt": "Create a guide on how to set up a home network with a router and Wi-Fi."
}
{
"prompt": "Write a short story about a time-traveling detective solving a mystery in ancient Rome."
}
Ensure the prompt is clear and specific. The more details you provide, the better the generated result will be. A well-structured prompt helps the model understand exactly what you want to achieve.
The prompt should not be too long. Try to keep it concise but informative. A prompt that is too long can confuse the model and lead to less accurate results.
Provide enough context to allow the model to generate relevant and coherent content. Context helps the model understand the tone, style, and subject of the content you desire.
Here is an example of how to send a POST request to the /generate-content endpoint with a prompt:
curl -X POST "http://127.0.0.1:8000/generate-content" -H "Content-Type: application/json" -d '{
"prompt": "Write an article about the future of renewable energy technologies."
}'
The response will be a JSON containing the generated article, for example:
{
"articles": [
{
"title": "Generated Article",
"description": "This article was generated based on the provided prompt.",
"content": "Renewable energy technologies are becoming increasingly important as the world seeks to reduce its dependence on fossil fuels..."
}
]
}
By following these guidelines, you will be able to structure effective prompts that will help the model generate high-quality content. If you have any other questions or need further clarification, do not hesitate to ask!
Brainverse-news-generator
This project was generated with Angular CLI version 18.2.7.
Run ng serve for a dev server. Navigate to http://localhost:4200/. The application will automatically reload if you change any of the source files.
Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.
Run ng build to build the project. The build artifacts will be stored in the dist/ directory.
Run ng test to execute the unit tests via Karma.
Run ng e2e to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
To get more help on the Angular CLI use ng help or go check out the Angular CLI Overview and Command Reference page.
Brainverse_Beta is the backend built on FastAPI. To run backend follow these steps:
This documentation describes the steps each team member should follow to configure and test the provided FastAPI code, which uses the C4AI Command R+ 08-2024 model. Follow these steps carefully to ensure the server and APIs function correctly.
Before you start, make sure you have:
API_TOKEN).fastapiuvicorntransformerstorch (or PyTorch)If you don't have them, you can install them with the command:
pip install fastapi uvicorn transformers torch
Create a virtual environment:
python -m venv .venv
.\.venv\Scripts\activate
python3 -m venv .venv
source .venv/bin/activate
Add the code to the main.py file:
main.py.main.py file.Configure your Hugging Face API token:
API_TOKEN with your valid token.Start the FastAPI server with Uvicorn:
uvicorn main:app --reload
main is the name of the file without the .py extension.--reload option allows the server to update automatically after code changes.Verify that the server is running:
http://127.0.0.1:8000.{
"message": "The server is up and running!"
}
Main Endpoint for Content Generation:
http://127.0.0.1:8000/generate-content{
"prompt": "Write an article about the impact of AI on education."
}
{
"articles": [
{
"title": "Generated Article",
"description": "This article was generated based on the provided prompt.",
"content": "Artificial intelligence is revolutionizing education by..."
}
]
}
Debug Endpoint (Optional):
http://127.0.0.1:8000/debug{
"prompt": "Test debug prompt"
}
{
"prompt_received": "Test debug prompt"
}
If you encounter an error related to PyTorch, install it with:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Ensure your Hugging Face token is correct and active. You can verify this by accessing your Hugging Face account.
/generate-content endpoint.If you need further clarification or support, don't hesitate to ask! π
Here is the complete code to be added to the main.py file:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
from transformers import AutoTokenizer, AutoModelForCausalLM
# Hugging Face model configuration
API_TOKEN = "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" # Replace with your valid token
MODEL_ID = "CohereForAI/c4ai-command-r-plus-08-2024"
try:
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_auth_token=API_TOKEN)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, use_auth_token=API_TOKEN)
print("Model loaded successfully!")
except Exception as e:
print(f"Error loading the model: {e}")
raise HTTPException(status_code=500, detail="Error loading the C4AI Command R+ 08-2024 model.")
# Define input/output models for FastAPI
class Article(BaseModel):
title: str
description: str
content: str
class ArticleResponse(BaseModel):
articles: List[Article]
class PromptInput(BaseModel):
prompt: str
# Initialize the FastAPI application
app = FastAPI()
# Function to generate content using the model
def generate_content(prompt: str) -> str:
try:
print(f"Received prompt: {prompt}") # Debug log
# Define conversation input:
conversation = [
{"role": "user", "content": prompt}
]
# Define tools available for the model to use:
tools = [
{
"name": "internet_search",
"description": "Returns a list of relevant document snippets for a textual query retrieved from the internet",
"parameter_definitions": {
"query": {
"description": "Query to search the internet with",
"type": 'str',
"required": True
}
}
},
{
'name': "directly_answer",
"description": "Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history",
'parameter_definitions': {}
}
]
# Render the tool use prompt as a string:
tool_use_prompt = tokenizer.apply_tool_use_template(
conversation,
tools=tools,
tokenize=False,
add_generation_prompt=True,
)
print(tool_use_prompt)
# Prepare the message for the model
messages = [{"role": "user", "content": tool_use_prompt}]
input_ids = tokenizer.apply_chat_template(
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
)
# Generate text
gen_tokens = model.generate(
input_ids,
max_new_tokens=1000, # Maximum number of generated tokens
temperature=0.7, # Controls the randomness of generation
top_p=0.9, # Nucleus filtering
do_sample=True, # Allows generation based on sampling
)
# Decode the generated result
gen_text = tokenizer.decode(gen_tokens[0], skip_special_tokens=True)
print(f"Generated response: {gen_text[:200]}...") # Log the first 200 characters
return gen_text
except Exception as e:
print(f"Error generating content: {e}")
raise HTTPException(status_code=500, detail=f"LLM model error: {e}")
# Endpoint to generate content
@app.post("/generate-content", response_model=ArticleResponse)
def generate_content_endpoint(prompt_input: PromptInput):
prompt = prompt_input.prompt
print(f"Received prompt via API: {prompt}")
try:
# Generate content
generated_content = generate_content(prompt)
except HTTPException as e:
raise e
except Exception as e:
print(f"Unknown error: {e}")
raise HTTPException(status_code=500, detail="Unknown error during content generation.")
# Create the article with the generated content
article = Article(
title="Generated Article",
description="This article was generated based on the provided prompt.",
content=generated_content
)
return ArticleResponse(articles=[article])
# Test endpoint
@app.get("/")
def read_root():
return {"message": "The server is up and running!"}
# Debug endpoint (optional)
@app.post("/debug")
def debug_endpoint(data: PromptInput):
print(f"Received request: {data}")
return {"prompt_received": data.prompt}
The model we are using requires a prompt structured in a specific format to generate high-quality content. Following these guidelines will help you achieve the best possible results. Here is how the prompt should be structured:
{
"prompt": "Write an article about the applications of artificial intelligence in healthcare."
}
Prompt Key: This key contains the text of the prompt that describes the content you want to generate. The text should be clear and specific to achieve the best results.
{
"prompt": "Write an article about the impact of artificial intelligence on healthcare."
}
{
"prompt": "Provide a detailed recipe for making a classic Italian lasagna."
}
{
"prompt": "Create a guide on how to set up a home network with a router and Wi-Fi."
}
{
"prompt": "Write a short story about a time-traveling detective solving a mystery in ancient Rome."
}
Ensure the prompt is clear and specific. The more details you provide, the better the generated result will be. A well-structured prompt helps the model understand exactly what you want to achieve.
The prompt should not be too long. Try to keep it concise but informative. A prompt that is too long can confuse the model and lead to less accurate results.
Provide enough context to allow the model to generate relevant and coherent content. Context helps the model understand the tone, style, and subject of the content you desire.
Here is an example of how to send a POST request to the /generate-content endpoint with a prompt:
curl -X POST "http://127.0.0.1:8000/generate-content" -H "Content-Type: application/json" -d '{
"prompt": "Write an article about the future of renewable energy technologies."
}'
The response will be a JSON containing the generated article, for example:
{
"articles": [
{
"title": "Generated Article",
"description": "This article was generated based on the provided prompt.",
"content": "Renewable energy technologies are becoming increasingly important as the world seeks to reduce its dependence on fossil fuels..."
}
]
}
By following these guidelines, you will be able to structure effective prompts that will help the model generate high-quality content. If you have any other questions or need further clarification, do not hesitate to ask!
Brainverse-news-generator
This project was generated with Angular CLI version 18.2.7.
Run ng serve for a dev server. Navigate to http://localhost:4200/. The application will automatically reload if you change any of the source files.
Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.
Run ng build to build the project. The build artifacts will be stored in the dist/ directory.
Run ng test to execute the unit tests via Karma.
Run ng e2e to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
To get more help on the Angular CLI use ng help or go check out the Angular CLI Overview and Command Reference page.