Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/just-cameron/mongo-hack.
Something went wrong. Try again.
mongo-hack fire_fun.py
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261from pathlib import Pathfrom fastapi import Depends, HTTPException, statusfrom fastapi.responses import FileResponsefrom modal import Stub, web_endpoint, Image, Secret, method, NetworkFileSystemimport osfrom typing import Dictimport json
from starlette.requests import Request
APP_NAME = "mongo-hack-fire-fun"
stub = Stub(name=APP_NAME)RESULTS_DIR = "/results"results_volume = NetworkFileSystem.from_name(f"{APP_NAME}-results-vol", create_if_missing=True)
image = ( Image.debian_slim( python_version="3.11" ).pip_install( "replicate", "python-jose", "requests", "openai", "scikit-learn", "numpy", "pymongo" ))
@stub.function(image=image, keep_warm=1, secrets=[Secret.from_name("mongo-hack-secret")])@web_endpoint(method="GET")def fetch_doc_text( docId: str, payload: Dict, request: Request):
print(docId)
from pymongo import MongoClient from bson.objectid import ObjectId
# Connect to MongoDB Atlas client = MongoClient("mongodb+srv://cameron:testing123@mongo-hackathon.4lo2sdu.mongodb.net/") db = client["mongo-hackathon"] collection = db["documents"]
print(collection)
# Specify the document ID document_id = docId
# Find the document by its ID document = collection.find_one({'_id': ObjectId(document_id)})
if document: print('Document found:') return document['text'] else: print('Document not found.') return document['Document not found.']
@stub.function(image=image, keep_warm=1, secrets=[Secret.from_name("mongo-hack-secret")])@web_endpoint(method="POST")def handle_request( payload: Dict, request: Request): import openai
client = openai.OpenAI( base_url = "https://api.fireworks.ai/inference/v1", api_key = os.getenv('FIREWORKS_API_KEY') )
fire_function__call_generate_title(client, "James runs a TV show and there are 5 main characters and 4 minor characters. He pays the minor characters $15,000 each episode. He paid the major characters three times as much. How much does he pay per episode? Let's be accurate as possible.")
fire_function_call(client)
def fire_function_call(client):
messages = [ { "role": "system", "content": "You are a very creative assistant with access to functions. Use them if required." }, { "role": "user", "content": "Select a super random topic and create a title and super detailed long text description for the topic" } ] tools = [ { "type": "function", "function": { "name": "get_title_text", "description": "Get the random text and title.", "parameters": { "type": "object", "properties": { "random_text": { "type": "string", "description": "random text", }, "title": { "type": "string", "description": "title for random text." }, }, "required": ["random_text", "title"], }, }, } ] chat_completion = client.chat.completions.create( model="accounts/fireworks/models/fw-function-call-34b-v0", messages=messages, tools=tools, tool_choice="auto", temperature=0.8 ) # print(repr(chat_completion.choices[0].message.model_dump()))
message_data = chat_completion.choices[0].message.model_dump()
# Assuming message_data now contains a dictionary with the 'tool_calls' key tool_calls = message_data['tool_calls']
# Extract the function arguments from the first tool call function_args_str = tool_calls[0]['function']['arguments']
# Parse the JSON string in function arguments function_args = json.loads(function_args_str)
# Extract 'random_text' and 'title' values random_text = function_args['random_text'] title = function_args['title']
print("Random Text:", random_text) print("Title:", title)
return random_text, title
def fire_function__call_generate_title(client, text):
messages = [ { "role": "system", "content": "You are an assistant good at creative titles with access to functions. Use them if required." }, { "role": "user", "content": f"create a very short title for following input text: {text}" } ] tools = [ { "type": "function", "function": { "name": "get_title", "description": "Geenerate title.", "parameters": { "type": "object", "properties": { "title": { "type": "string", "description": "title for input text." }, }, "required": ["title"], }, }, } ] chat_completion = client.chat.completions.create( model="accounts/fireworks/models/fw-function-call-34b-v0", messages=messages, tools=tools, tool_choice="auto", temperature=0.1 ) print(repr(chat_completion.choices[0].message.model_dump()))
message_data = chat_completion.choices[0].message.model_dump()
# Assuming message_data now contains a dictionary with the 'tool_calls' key tool_calls = message_data['tool_calls']
# Extract the function arguments from the first tool call function_args_str = tool_calls[0]['function']['arguments']
# Parse the JSON string in function arguments function_args = json.loads(function_args_str)
# Extract 'random_text' and 'title' values title = function_args['title']
print("Title:", title)
return title
def fire_works_create_title(text): client = openai.OpenAI( base_url = "https://api.fireworks.ai/inference/v1", api_key=os.getenv('FIREWORKS_API_KEY'), ) response = client.chat.completions.create( model="accounts/fireworks/models/llama-v2-7b-chat", messages=[{ "role": "user", "content": f"create a title for this text: {text}", }], ) print(response.choices[0].message.content)
return response.choices[0].message.content
@stub.function(image=image, keep_warm=1, secrets=[Secret.from_name("mongo-hack-secret")])@web_endpoint(method="GET")def insert_titles():
from pymongo import MongoClient from bson.objectid import ObjectId
# Connect to MongoDB Atlas client = MongoClient("mongodb+srv://cameron:testing123@mongo-hackathon.4lo2sdu.mongodb.net/") db = client["mongo-hackathon"] collection = db["documents"]
print(collection)
# Retrieve all documents in the collection documents = collection.find()
for document in documents: # Get the title of the document print(document) title = document.get("title", "No title available") # Get the document ID document_id = str(document["_id"]) print(f"Document ID: {document_id}")