Untitled

mail@pastecode.io avatar
unknown
python
a month ago
2.7 kB
2
Indexable
Never
import os
import sys
import json
import pandas as pd
from pydantic import BaseModel, Field
from typing import List
import importlib
from idea_generation import get_prj_root

from langchain import PromptTemplate
from langchain.utils.openai_functions import convert_to_openai_function
from langchain.output_parsers.openai_functions import JsonKeyOutputFunctionsParser

def get_config(config_file_path):
    """Loads configuration from a JSON file."""
    with open(config_file_path, 'r') as f:
        config = json.load(f)
    return config

def load_data(data_file_path, priority=1):
    """Loads data from a CSV file and filters by priority."""
    df = pd.read_csv(data_file_path)
    df = df[df["priority"] == priority]
    return df

def save_result(result, save_dir, industry_id, industry_name):
    """Saves the result as a JSON file."""
    filename = f"{industry_id}_{industry_name}_vc.json"
    save_path = os.path.join(save_dir, filename)
    with open(save_path, 'w') as f:
        json.dump(result, f, ensure_ascii=False, indent=4)
    return save_path

class ValueChainStep(BaseModel):
    vcstep_number: int = Field(description="value chain step number")
    vcstep_name: str = Field(..., description="concise name of value chain step")
    vcstep_description: str = Field(..., description="concise overview description of value chain step(20 - 50 words)")

class ListValueChainStep(BaseModel):
    value_chains: List[ValueChainStep] = Field(description="break down of value chain steps")

def vc_breakdown(industry):
    # ... (rest of the vc_breakdown function remains the same)

def process_vc_breakdown(df, save_dir):
    list_save_path = []
    for ix, row in df.iterrows():
        input_industry_info = row[input_cols].to_dict()
        response = vc_breakdown(input_industry_info)

        dict_result = {}
        dict_result["industry_info"] = row.to_dict()
        dict_result["value_chains"] = response

        save_path = save_result(dict_result, save_dir, row["industry_id"], row["industry_name"])
        list_save_path.append(save_path)

    return list_save_path

if __name__ == "__main__":
    pj_base_dir = get_prj_root()
    sys.path.append(os.path.join(pj_base_dir, '0000_system/0100_modules'))

    config_file_path = os.path.join(pj_base_dir, "config.json")
    config = get_config(config_file_path)

    definition_load_path = config["data_paths"]["industry_definition"]
    save_dir = config["output_paths"]["value_chain"]

    df_definition = load_data(definition_load_path)
    result = process_vc_breakdown(df_definition[:1], save_dir)
Leave a Comment